Hi All:
I'm a new one to the DRF3, i want to know how to dynamically modifying fields.
i follow the link: http://www.django-rest-framework.org/api-guide/serializers /#dynamically-modifying-fields .
that's mean i can't use the ModelViewSet ?
the fields is from the request params ,is there any way get the request params like this (red color):
class DynamicFieldsModelSerializer(serializers.ModelSerializer):"""A ModelSerializer that takes an additional `fields` argument thatcontrols which fields should be displayed."""
def __init__(self, *args, **kwargs):# Instantiate the superclass normallysuper(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
fields = self.request.get('fields')no_fields = self.request.get('nofields')
if fields is not None:# Drop any fields that are not specified in the `fields` argument.allowed = set(fields[1:-1].split(','))existing = set(self.fields.keys())for field_name in existing - allowed:self.fields.pop(field_name)
if no_fields is not None:for field_name in set(no_fields[1:-1].split(',')):self.fields.pop(field_name)
--
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.
在 2014年12月25日,下午10:28,Zoltan Szalai <defau...@gmail.com> 写道:pagination
--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/m4wB5VxfcjQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.

class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def __init__(self, *args, **kwargs):
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
request = self.context.get('request')
print request
fields = request.query_params.get('fields')
if fields is not None:
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed. The argument comes
from the request object.
"""
def __init__(self, *args, **kwargs):
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
request = self.context.get('request', None)
if request and request.method == 'GET':
fields = request.query_params.get('fields', None)
if fields is not None:
allowed = set(fields.split(','))
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/m4wB5VxfcjQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
if request and request.method == 'GET':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.
--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/m4wB5VxfcjQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsubscri...@googlegroups.com.