Comments (8)
Ah. Try:
bookmarks = BookmarkSerializer(many=True, attr="bookmarks.all", call=True)
from serpy.
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.
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.
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.
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.
Got it thank you
from serpy.
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.
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)
- Can't install serpy from source
- Remove Python 2.6 from testing matrix HOT 1
- Methods are not overridden by the child class's implementation HOT 3
- Patch for compatibility with RelatedManager (reverse foreign-key) HOT 2
- Inconsistent: child serializer required=False returns None HOT 2
- Exceptions are not easy to trace
- DateField is not supported HOT 2
- Non-required key raises key error when not present HOT 2
- Expose fields such that swagger can autogenerate models/code? HOT 2
- Serializing properties which may or may not be present in object being passed in HOT 2
- serpy no longer supports None as field values HOT 1
- No way to omit field from output HOT 6
- Nested 'self' field in Serializer HOT 2
- Accessing request object in method field - solution not working?
- Is it still under active development? HOT 2
- include test suite in PyPi tarball
- Django GeoJSON / Geometry fields. HOT 1
- Serpy does not catch Key and Attribute errors on MethodField
- is this library no longer maintained ? HOT 1
- Type Hints
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serpy.