I have two linked instances that should always be created together. I would like to do this without using signals or override the save() model method.
class Car(models.Mode): make = models.CharField(max_length=32) model = models.CharField(max_length=32) class Meta: unique_together = ('make', 'model',) objects = CarManager() class CarProfile(models.Model): car = models.OneToOneField(Car) last_checkup = models.DateTimeField(blank=True, null=True)
I created a custom CarManager that overrides models.Manager.create () to ensure that CarProfile is created when creating the car:
class CarManager(models.Manager): def create(self, **kwargs): with transaction.atomic(): car = self.model(**kwargs) car.save(force_insert=True) CarProfile.objects.create(car=car) return car
When I call Car.objects.create(make='Audi', model='R8') , a new Car instance is created and its corresponding CarProfile. However, when I try to create a new car using Car.objects.update_or_create(make='Audi', model='R8') or Car.objects.get_or_create(make='Audi', model='R8') , in both In cases, a Car instance is created, but the corresponding CarProfile is not created.
Why don't update_or_create and get_or_create call the expected CarProfile instance when I specified this behavior in a custom create() method ?
It seems that both of these methods call the create() class from QuerySet instead of my custom one.
lajitong
source share