Monday, 8 August 2022

Django model inheritance - creating child objects based on parent instance

I am using Django 4.0

I have the following models:

class Parent(models.Model):
    # fields ommitted for the sake of brevity
    pass

class Child(Parent):
   child_only_field = models.CharField(max_length=64)

code

p = Parent.objects.create(**fields_dict)
c1 = Child.objects.create(child_only_field='Hello 123', p) # Obviously won't work
# c2 = ...

Is there a way to create child objects from an instance of a parent (without manually "unpacking" the fields of the parent)?



from Django model inheritance - creating child objects based on parent instance

No comments:

Post a Comment