Code Monkey home page Code Monkey logo

Comments (8)

ahankinson avatar ahankinson commented on August 17, 2024 7

Ah. Try:

bookmarks = BookmarkSerializer(many=True, attr="bookmarks.all", call=True)

from serpy.

ahankinson avatar ahankinson commented on August 17, 2024
class BookmarkSerializer(serpy.Serializer):
    id = serpy.IntField()
    title = serpy.CharField()
    url = serpy.MethodField()

class PersonSerializer(serpy.Serializer):
    bookmarks = BookmarkSerializer(many=True)

Does that not work?

from serpy.

adampotocki avatar adampotocki commented on August 17, 2024

I tried that but when I do that I get:

return [serialize(o, fields) for o in instance]
TypeError: 'RelatedManager' object is not iterable

Am I doing something wrong in the DRF ModelViewSet? Can I use the DRF ModelViewSet like I'm using it above?

from serpy.

adampotocki avatar adampotocki commented on August 17, 2024

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.

from serpy.

ahankinson avatar ahankinson commented on August 17, 2024

I've not tried model view sets with it so I'm not sure.

When all else fails, I fall back to the method field:

bookmarks = serpy.MethodField()

def get_bookmarks(self, obj):
    # do interesting stuff with obj
    return somethinguseful

from serpy.

adampotocki avatar adampotocki commented on August 17, 2024

Got it thank you

from serpy.

jackton1 avatar jackton1 commented on August 17, 2024

Another solution is to change the as_getter.

Can also check the type(value.__class__.__name__) == 'RelatedManager' since its a local in the create_reverse_many_to_one_manager function wouldn't recommend it.

from functools import partial

from serpy import Serializer

class BaseSerializer(Serializer):
    def as_getter(self, serializer_field_name, serializer_cls):
        return partial(get_related, serializer_field_name)

def get_related(name, instance):
    value = getattr(instance, name, None)
    if value and is_related(instance._meta, value):
        value = value.all()
    return value

def is_related(opts, value):
    field = None
    if hasattr(value, 'field'):
        field = value.field
    if not field and hasattr(value, 'source_field'):
        field = value.source_field
    if field and hasattr(field, 'remote_field'):
        return (
            field.remote_field in related_fields(opts) or value.field in many_to_many_fields(opts)
        )
    return False

def related_fields(opts):
    return (
        f for f in opts.get_fields(include_hidden=True)
        if f.auto_created and not f.concrete and (f.one_to_one or f.one_to_many)
    )

def many_to_many_fields(opts):
    return (
        f for f in opts.get_fields(include_hidden=True)
        if f.many_to_many and f.auto_created
    )

from serpy.

edouarobin avatar edouarobin commented on August 17, 2024

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

from serpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.