ForeignKey to specific queryset django - python

Is there a way to refer to specific object of Model? Suppose I have some models like below:
# models.py
class VehicleCategoryCode(models.Model):
category = models.CharField(max_length=5)
name = models.CharField(max_length=20)
class Code(models.Model):
category = models.ForeignKey(VehicleCategoryCode, on_delete=models.CASCADE)
index = models.CharField(max_length=4, blank=True)
label = models.CharField(max_length=50)
order = models.CharField(max_length=20, blank=True)
is_active = models.BooleanField(default=True)
# pay attention to the Model
class Vehicle(models.Model):
label = models.CharField(max_length=80)
model = models.CharField(max_length=30)
Currently Vehicle is not linked to any model.
Now Code model is ForeignKey to VehicleCategoryCode, which has two objects. In the VehicleCategoryCode the first object label (for convenience sake) will be referenced by Vehicle.label, and the second object model (once again for convenience) will be referenced by Vehicle.model. So each field in Vehicle can refer to the same model, but different objects.
So basically I'm wondering if something like the pseudo code below can be achieved anyhow.
class Vehicle(models.Model):
label = models.ForeignKey(VehicleCategoryCode__name='label', on_delete=models.CASCADE)
model = models.ForeignKey(VehicleCategoryCOde__name='model', on_delete=models.CASCADE)
Any suggestion or advice would be appreciated. Thank you.

You can make use of the limit_choices_to=… parameter [Django-doc]:
Vehicle(models.Model):
label = models.ForeignKey(
Code,
limit_choices_to={'category__name': 'label'},
on_delete=models.CASCADE
)
model = models.ForeignKey(
Code,
limit_choices_to={'category__name': 'model'},
on_delete=models.CASCADE
)
For ModelForms and in the ModelAdmin it will limit the choices, note however that tese are not enforced by the database.

Related

Django Model Instance as Template for Another Model that is populated by Models

I'm trying to create a workout tracking application where a user can:
Create an instance of an ExerciseTemplate model from a list of available Exercise models. I've created these as models so that the user can create custom Exercises in the future. There is also an ExerciseInstance which is to be used to track and modify the ExerciseTemplate created by the user, or someone else. I'm stripping the models of several unimportant fields for simplicity, but each contains the following:
class Exercise(models.Model):
# Basic Variables
name = models.CharField(max_length=128)
description = models.TextField(null=True, blank=True)
def __str__(self):
return self.name
class ExerciseTemplate(models.Model):
# Foreign Models
workout = models.ForeignKey(
'Workout',
on_delete=models.SET_NULL,
null=True,
blank=True
)
exercise = models.ForeignKey(
Exercise,
on_delete=models.SET_NULL,
null=True,
blank=True
)
recommended_sets = models.PositiveIntegerField(null=True, blank=True)
class ExerciseInstance(models.Model):
""" Foreign Models """
exercise_template = models.ForeignKey(
ExerciseTemplate,
on_delete=models.SET_NULL,
null=True,
blank=True
)
workout = models.ForeignKey(
'Workout',
on_delete=models.SET_NULL,
null=True,
blank=True
)
""" Fields """
weight = models.PositiveIntegerField(null=True, blank=True)
reps = models.PositiveIntegerField(null=True, blank=True)
Create a WorkoutInstance from a WorkoutTemplate. The WorkoutTemplate is made up of ExerciseTemplates. But the WorkoutInstance should be able to take the WorkoutTemplate and populate it with ExerciseInstances based on the ExerciseTemplates in the WorkoutTemplate. Here are the models that I have so far:
class WorkoutTemplate(models.Model):
name = models.CharField(max_length=128)
description = models.TextField(null=True, blank=True)
date = models.DateTimeField(null=True, blank=True)
#category...
exercises = models.ManyToManyField(
Exercise,
through=ExerciseTemplate
)
def __str__(self):
return self.name
class WorkoutInstance(models.Model):
# Foreign Models
workout_template = models.ForeignKey(
'WorkoutTemplate',
on_delete=models.SET_NULL,
null=True,
blank=True
)
But this is where I get stuck. I'm not sure how to proceed. My intuition is one of the following:
I need to create a more simple architecture to do this. I'll take any suggestions.
I need to create a method within the model that solves this issue. If this is the case, I'm not sure what this would actually look like.
When you create a new WorkoutInstance object which references a given WorkoutTemplate object you get all its related ExerciseTemplate objects.
Then you just create a new object (row) for each ExerciseInstance in another model (table)
If you link your ExerciseInstance to WorkoutInstance via 'workout' you could do something like:
wt = WorkoutTemplate.get(id=1)
wi = WorkoutInstance.create(workout_template=wt)
for e in wt.exercisetemplate_set.all:
ExerciseInstance.create(exercise_template=e, workout=wi)
You can implent this in the method that creates the new WorkoutInstance or take a look at signals
https://docs.djangoproject.com/en/4.0/topics/db/optimization/#create-in-bulk

Foreign keys and serializers in django rest

class Patient(models.Model):
user = models.OneToOneField(User, related_name='patient', on_delete=models.CASCADE)
id_type = models.CharField(max_length=300)
id_number = models.CharField(max_length=300)
creation_date = models.DateField(default=datetime.date.today)
class Allergie(models.Model):
name = models.CharField(max_length=300, default="X")
class PatientAllergies(models.Model):
patient = models.ForeignKey(Patient, related_name="patient_allergies", on_delete=models.CASCADE)
allergie = models.ForeignKey(Allergie, on_delete=models.CASCADE, null=True)
professional_contract = models.ForeignKey(ProfessionalContract, null=True ,on_delete=models.CASCADE)
Is it possible to retrieve a patient objecto with a property that is a list of all his allergies, including name and id with these models?
you have the PatientAllergies as a chain,
so
patientAllergies = PatientAllergies.objects.get(patient.id_number='0000')
patientAllergies.allergie #you get the single allergie model connect with it, take care it is a foreignKey so it is singolar and not many
patientAlleriges.patient.user #will give you access to all the data of the user
You can achieve this with prefetch_related and Prefetch like so:
Patient.objects.prefetch_related(
Prefetch('patient_allergies__allergie', to_attr='allergies')
)
EDIT: Just learned that to_attr will not work on multiple levels of prefetch. Another approach I can think of is use a model property for Patient that returns its related allergies like this:
class Patient(models.Model):
#property
def allergies(self):
return Allergie.objects.filter(patientallergies_set__patient=self)
Then in your serializer, the allergies field can use the Allergies serializer

How can I create a ForeignKey leaving chances for different models for the TO field?

I have multiple product models like below
class ProductBase(models.Model):
name = models.CharField(max_length=50)
class Meta:
abstract = True
def __str__(self):
return self.name
class ProductOne(ProductBase):
color = models.CharField(max_length=50)
class ProductTwo(ProductBase):
type = models.CharField(max_length=50)
class ProductThree(ProductBase):
breed = models.CharField(max_length=50)
Each of these products is expected to have an image_set, so I created the model below for product's images
class Image(models.Model):
product = models.ForeignKey(to=[HERE IS THE PROBLEM], on_delete=models.CASCADE)
image = models.ImageField(upload_to='upload-path')
How can I get the product field in my Image class to point to any of the products defined above as desired.
class Image(models.Model):
product = models.ForeignKey(ProductOne, on_delete=models.CASCADE)
image = models.ImageField(upload_to='upload-path')
there is no 'to' attribute you just refer it to the model that you want
It would be better to have just one model for all the products, for example:
class Product(ProductBase):
model = models.CharField(max_length=50) #here for example you put the individual info from the different types of products
color = models.CharField(max_length=50, null=True) #add null=True so you don't have to add it for all the products
etc...
Then you don't need a separate Image model, you can just add it as a field to your Product model. I believe this is a cleaner way to implement it, as there is no real need to have multiple models in this case.

Django order objects by specific related object field

In my project there is an Attraction model and an AttractionTag model related through a ForeignKey relationship. Every Attraction has the same set of AttractionTag, they differ only by the value field.
Now, I want to order Attractions based on the value field of a specific AttractionTag. For example, there is an AttractionTag named 'modern' for every attraction. I want to order Attractions based on modern AttractionTag value field.
I've tried
attractions.order_by('-attractiontag__value')
but this command order Attractions on AttractionTag in general, not based on a particular AttractionTag.
Here are the models
class Attraction (models.Model) :
city = models.ForeignKey(City, on_delete=models.CASCADE)
name=models.CharField(max_length=50, unique=True)
image = models.ImageField(upload_to=attractionImagePath, null=True, blank=False)
imageTop = models.ImageField(upload_to=attractionTopImagePath, null=True, blank=True)
pub_date = models.DateTimeField(auto_now_add=True)
class AttractionTag (models.Model):
attraction=models.ForeignKey(Attraction, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
value=models.IntegerField(default=0)
How can I solve?
Thank you
Ok, I found a solution.
tags = AttractionTag.objects.filter(attraction__city=city)
tags = tags.filter(name='modern').order_by('-value')
attraction = [tag.attraction for tag in tags]
Should work

django model field depend on the value of another field

The use case of my application is I will have various fields to fill and among them one is Industry field and another is Segment Field for brand. The industry field is like category that brand falls into. So, if i choose the industry as Health Care for XYZ brand then the segment field should show the items like 'Ayurveda', 'Dental Clinics' (all health care related items). Basically, its like sub-category.
Here is a sample model
class Industry(models.Model):
name = models.CharField(max_length=150, blank=True, null=True)
class Meta:
verbose_name = 'Industry'
verbose_name_plural = 'Industries'
def __str__(self):
return self.name
class Segment(models.Model):
industry = models.ForeignKey(Industry, related_name='segment', on_delete=models.CASCADE)
name = models.CharField(max_length=150, blank=True, null=True)
class Meta:
verbose_name = 'Segment'
verbose_name_plural = 'Segments'
def __str__(self):
return f'{self.industry.name} - {self.name}'
class BusinessModel(models):
industry = models.ForeignKey(Industry, blank=False, null=False, related_name='industry', on_delete=models.CASCADE)
# segements = models.ForeignKey()
total_investment = models.CharField() # will be choice field
This is a simple model and I have not created Segment model as I am not sure how to approach to this problem. I am just curios to know, if for such case, do i have to something special in models.py or in the view side. Such type of things get arise during development phase, thus, I want to be clear on problem solving pattern in django.
UPDATE
https://www.franchisebazar.com/franchisor-registration here if you choose industry inside Business model section, the segment will be updated accordingly.
You can have a 3 model design like
class Industry(models.Model):
name = models.CharField(max_length=150, blank=True, null=True)
class Segment(models.Model):
name = models.CharField(max_length=150, blank=True, null=True)
class Mapping(models.Model):
industry = models.ForeignKey(Industry)
segment = models.ForeignKey(Segment)
You need to define relations between your models. You can find documentation about ManyToMany relation here which is suitable in your case.
you can use ChainedForeginKey.. Check the below links
customizing admin of django to have dependent select fields
https://django-smart-selects.readthedocs.io/en/latest/installation.html

Categories

Resources