Hi,
Can you help me how to disable creating nested objects ?
I have serializers like this:
(Employee has ForeignKey to Team)
class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = ('id', 'name')
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = ('id', 'name', 'surname', 'team')
depth = 1
or instead could be:
class EmployeeSerializer(serializers.ModelSerializer):
team = TeamSerializer()
class Meta:
model = Employee
fields = ('id', 'name', 'surname', 'team')
when i post json (create employee)
{
name: "name",
surname: "surname",
team: {
id: 1,
name: "Web Team"
}
}
object employee creates but also object team... is there any way to disable creating team object together with employee ? i just want to create employee and set selected team (curently in database) to employee
And on GET (list) i would like to be able to retrieve data like:
{
name: "name",
surname: "surname",
team: {
id: 1,
name: "Web Team"
}
not like that
{
name: "name",
surname: "surname",
team: 1
}
Is there any way to to that in django rest framework (also iam using angular)
Regards
UPDATE
Currently serializers:
class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
class EmployeeSerializer(serializers.ModelSerializer):
team = TeamSerializer()
class Meta:
model = Employee
I cant use serializers.RelatedField() because it's returned only unicode I need 'id' and 'name' (i suppose)
this is my POST json: ( Restangular.all('employee').post(data) )
data: {
name: "emp1",
photo: "",
skype: "",
surname: "qweqwe",
team: {
id: 1,
name: "Web",
}
}
and DRF returned json:
employee: {
id: 2,
name: "emp1",
photo: "",
skype: "",
surname: "qweqwe",
team: {
id:3, <-- NEW ID!
name: "Web"
}
}
so yea, i am sure that new team objects created. So what now ?:)
Solved problem:
post/put method use primary key (WriteEmployeeSerializer) - before replace dict to primary key
get method use full object (ReadEmployeeSerializer)
I found that
ModelSerializer.to_native()
andModelSerializer.from_native()
don't exist in the latest version of DRF. I came up with the following derived from the accepted solution:Effectively, it seems that
from_native
is nowto_representation
, andto_native
is noto_internal_value
Okay, first of all, are you sure that your nested object is created? Because DRF was not designed to create nested objects, so this is a very strange behavior (more precisely, this is a work in progress, as stated by its creator, Tom Christie).
Then, in order to have the serializer representation that you want, you must follow some rules:
Create a simple serializer for each model (just like in your first code snippet)
Add the FK relationship on the EmployeeSerializer: (be careful, for this to work, you must have your FK named 'team') team = serializers.RelatedField()
Also, you should remove the depth attribute from your serializer, he is the one that flattens your serialization (or you can just set it to 2). Hope this helps.
UPDATE
View for multiple serializers:
A bit redundant, but should do the job.