I have an issue when trying to create nested objects, more specifically creating a parent and its child at the same time.
The child's model has the parent's id as foreign key as can be seen below.
Here is my parent_model
:
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
from PIL import Image
class Work(models.Model):
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
name = models.CharField(max_length=200)
length = models.IntegerField(null=True)
width = models.IntegerField(null=True)
def __str__(self):
return "{}".format(self.id)
My child_model
:
from django.db import models
from .model_work import *
from .model_taxes import *
from djmoney.models.fields import MoneyField
class Price(models.Model):
work = models.OneToOneField(Work, on_delete=models.CASCADE, related_name='price')
price = MoneyField(max_digits=19, decimal_places=4, default_currency='USD', null=True)
total = models.IntegerField(null=True)
def __str__(self):
return "{}".format(self.price)
Here is my ParentCreateSerializer
:
class WorkCreateSerializer(serializers.ModelSerializer):
"""
Serializer to create a new Work model in DB
"""
price = PriceCreateSerializer()
class Meta:
model = Work
fields = [
'user',
'price',
'name',
'length',
'width'
]
def create(self, validated_data):
price_data = validated_data.pop('price')
work = Work.objects.create(**validated_data)
price = Price.objects.create(**price_data)
return work
My ChildCreateSerializer
:
class PriceCreateSerializer(serializers.ModelSerializer):
"""
Serializer to create a new Price when new Work model is created in DB
"""
# work = WorkDetailsSerializer()
class Meta:
model = Price
fields = [
'work',
'price',
'price_currency',
'total'
]
def create(self, validated_data):
work_data = validated_data.pop('work')
work = Work.objects.create(**work_data)
price = Price.objects.create(**validated_data)
return price
When I POST an object as shown below, both the parent and child objects are created but I can't manage giving the child the parent's id as foreign key, so they are not linked.
I have tried linking the child's create serializer to the parent's detail serializer (the commented line in my ChildCreateSerializer
) but that creates an error work = WorkDetailsSerializer() NameError: name 'WorkDetailsSerializer' is not defined
.
Due to the serializers initialisation, because it seems this creates an infinite loop as explained in this Django: Creating Nested Objects with Reverse Relationship post.
{
"user":2,
"price":
{
"price":20,
"price_currency":"EUR",
"total":32
},
"name":"work 42",
"length":"50",
"width":"60",
}
Here is the result:
{
"id": 33,
"user": {
"id": 2,
"username": "Max",
"password": "pbkdf2_sha256$180000$WXTaxmhOOTZF$oTx2i/HoZk+lCxHWsRYGVVZcw3/Sy8Micc4YOfaDRaM="
},
"price": null,
"name": "work 42",
"length": 50,
"width": 60
}
I've noticed that I don't enter the "create()" method of the child's serializer.
Does anyone know how to pass to the child the parent's id as foreign key?
Is that done in the "create()" method, and if yes then how can I access it?