Reputation: 2316
Say I have a car model that looks like this:
class Car(models.Model):
car_model = models.CharField(max_length=100)
car_number = models.IntegerField()
and I also have a wheel model that now looks like this:
class Wheel(models.Model):
car = models.ForeignKey(Car, on_delete=models.CASCADE)
I want to specify a wheel's model so it has to be the same like its car's model.
something like that:
class Wheel(models.Model):
car = models.ForeignKey(Car, on_delete=models.CASCADE)
wheel_model = car.car_model
How can I achieve this? So If my car_model is BMW, I want also to have wheel_model BMW set automatically
Upvotes: 0
Views: 30
Reputation: 1203
You can define a property wheel_model
on Wheel
.
class Wheel(models.Model):
car = models.ForeignKey(Car, on_delete=models.CASCADE)
@property
def wheel_model(self):
return self.car.car_model
Now you can access it
wheel_obj.wheel_model
Upvotes: 2