nepherhotep / django-rest-framework-dyn-serializer Goto Github PK
View Code? Open in Web Editor NEWDynamic serializer for DRF to allow REST endpoint to return model fields on demand
Dynamic serializer for DRF to allow REST endpoint to return model fields on demand
Hello,
I am using Django rest framework 3.6.4 and I am getting the following error:
File "/home/vagrant/.virtualenvs/api/lib/python3.6/site-packages/rest_framework_dyn_serializer.py", line 44, in init
field_name.context = self.context
AttributeError: can't set attribute
I think that the error is related to the following change in the version 3.6.4:
encode/django-rest-framework@563020e
You can see in the line 599 that @cached_property is not longer used, and it was replaced for @Property, I think that the (for in ) in line https://github.com/Nepherhotep/django-rest-framework-dyn-serializer/blob/master/rest_framework_dyn_serializer.py#L43 is not need anymore, but I am not totally sure.
Regards.
Here is an example taken from test with a little change
# Pass limit_fields = False to nested serializer
class ArticleDynSerializer(DynModelSerializer):
author = AuthorDynSerializer(required=False, limit_fields=False)
class Meta:
model = Article
fields_param = 'article_fields'
fields = ['id', 'title', 'created', 'updated', 'content', 'author']
class ArticleViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Article.objects.all().order_by('id')
def get_serializer(self, *args, **kwargs):
context = self.get_serializer_context()
context['request'] = self.request
s = ArticleDynSerializer(*args, context=context, limit_fields=True, **kwargs)
return s
In such case author serializer behaves like dynamic in spite of limit_fields is set to False. So all fields for nested serializers always must be defined explicitly in the request.
what is self.nested
Is it possible to limit fields when declared so that a model serializer can be re-used, e.g:
UserModelSerializer(fields=('id', 'first_name', 'last_name',))
This way I can re-use the UserModelSerializer in other places without exposing additional info such as username, email etc when nesting it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.