Losing foreign key value in Django REST serializer

1,925 views
Skip to first unread message

Alexander Teut

unread,
May 29, 2018, 11:13:11 AM5/29/18
to Django REST framework

I've simplified my code to make problem more clear.

I have two related models in my Django REST project. In my models.py:

class Project(Models.model):
    name = models.CharField(max_length=150)
class Item(models.Model):
    project = models.ForeignKey(Project, blank=True, on_delete=models.CASCADE)
    data = models.TextField(blank=True, null=True)

Then I create a serialiser:

class ItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = Item
        fields = ('id', 'project_id', "data")

View set:

class ItemViewSet(viewsets.ModelViewSet):
    queryset = Item.objects.all()
    serializer_class = ItemSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('project_id')

Router:

router.register(r'items', ItemViewSet)

This implementation works fine for models without foreign keys. But when I post something like:

{
    project_id: 1,
    data: "text"
}

(the project with id=1 exists)

my ItemSerializer gets just { data: "text" } to validated_fields.

The project_id field is completely lost during validation.

How to configure Serializer or ViewSet to make it workable?

Xavier Ordoquy

unread,
May 29, 2018, 12:07:23 PM5/29/18
to django-res...@googlegroups.com
Hi,

In your serializer and your payload, replace « project_id » with « project ».
DRF expects same named relation fields to be serialized/deserialized by default.

project_id exists in the model so it’s accepted by DRF but since it has no field in the model associated (it’s a property) it’ll be a ReadOnlyField.

Regards,
Xavier Ordoquy,
Linovia


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Teut

unread,
May 30, 2018, 4:32:19 AM5/30/18
to Django REST framework
Thank you!

I fixed it this way:

class ItemSerializer(serializers.ModelSerializer):
    research = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())
    class Meta:
        model = Item
        fields = ('id', 'project', "data")

вторник, 29 мая 2018 г., 19:07:23 UTC+3 пользователь Xavier Ordoquy написал:
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Pallavi Singh

unread,
May 30, 2018, 2:59:30 PM5/30/18
to django-res...@googlegroups.com
i am new for drf,and i m trying to do forget password ,how can i do?


To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Francisco Pandol

unread,
May 31, 2018, 8:56:23 AM5/31/18
to django-res...@googlegroups.com
Pallavi you can use https://github.com/sunscrapers/djoser
But the next time please create a new thread for an unrelated question.
--
Francisco Pandol

Pallavi Singh

unread,
Jun 5, 2018, 1:43:07 PM6/5/18
to Django REST framework
how to create a new thread for an unrelated questions?will u pls guide me?


Francisco Pandol
Reply all
Reply to author
Forward
0 new messages