question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to serialize django model using serpy?

See original GitHub issue

Kinda new to python and django so I apologize if this is a noobish question. How would I serialize this following related models using Serpy instead of DRF ModelSerializer and then use the serpy serializer in a DRF ModelViewSet?

Models:

class Person(models.Model):
    first_name = models.CharField(_("First Name"), max_length=128, null=True)
    middle_name = models.CharField(_("Middle Name"), max_length=128, blank=True, null=True)
    last_name = models.CharField(_("Last Name"), max_length=128, null=True)
    email = models.EmailField(_("Email"), blank=True, null=True)

    def __unicode__ (self):
        return '%s: %s %s %s' % (self.id, self.first_name, self.middle_name, self.last_name)

class Bookmark(models.Model):
    person = models.ForeignKey(Person, related_name='bookmarks', on_delete=models.CASCADE)
    title = models.CharField(_("Title"), max_length=9999, blank=True, null=True)
    url = models.URLField(_("Url"), max_length=9999, blank=True, null=True)

    def __unicode__ (self):
        return '%s %s' % (self.title, self.url)

DRF ModelSerializers:

class BookmarkSerializer(ModelSerializer):
    class Meta:
        model = Bookmark
        fields = ('id', 'title', 'url', 'person')

class PersonSerializer(ModelSerializer):
    bookmarks = BookmarkSerializer(many=True, read_only=True)

    class Meta:
        model = Person
        fields = ('id', 'first_name', 'middle_name', 'last_name', 'email', 'bookmarks')

DRF ModelViewSet:

class PersonViewSet(viewsets.ModelViewSet):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
ahankinsoncommented, Mar 10, 2016

Ah. Try:

bookmarks = BookmarkSerializer(many=True, attr="bookmarks.all", call=True)
0reactions
edouarobincommented, Sep 10, 2020

Hmm seemed to get passed that, now getting the following when I try to view my persons:

AttributeError: 'NoneType' object has no attribute 'last_name'

I’m not sure if I’m allowed to use DRF ModelViewSet with serpy?

Thank you for your help btw Andrew.

Set required=False

Read more comments on GitHub >

github_iconTop Results From Across the Web

serpy: ridiculously fast object serialization — serpy 0.3.1 ...
serpy is a super simple object serialization framework built for speed. serpy serializes complex datatypes (Django Models, custom classes, …) ...
Read more >
How to serialize a django model using serpy package
I'm trying to serialize the default User model using the serpy package but no success. Serializers class GroupSerializer(serpy.
Read more >
drf-serpy - PyPI
drf-serpy is a super simple object serialization framework built for speed. drf-serpy serializes complex datatypes (Django Models, ...
Read more >
Tutorial 1: Serialization - Django REST framework
We can do this by declaring serializers that work very similar to Django's forms. Create a file in the snippets directory named serializers.py...
Read more >
JSON Serialization in Python using serpy - Twilio
The simplest thing to do here would be to separate the serializer definition from the original class definition. This is where serpy comes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found