I have one model serializer class with one extra field in it and i want to serialize that. Below is my serilizer..
from rest_framework import serializers from .models import WUser class UserTypeSerializer(serializers.Serializer): user_type = serializers.IntegerField(min_value=0, max_value=8, default=0) class BaseUserSerializer(serializers.ModelSerializer): user_type = UserTypeSerializer() class Meta: model = WUser fields = ('user_type', 'username', 'email', 'first_name', 'last_name', 'password', 'mobile_no')
i am calling this serailizer with below method..
serialized_data = BaseUserSerializer(user, data={"user_type": 0, "password": password})
But this is not working..
I want serialized output in nested JSON form that is the reason i created another serializer.
Required Output
"user_type": 0, "user": { "username": "ABCED", "first_name": "Deendayal", "last_name": "Garg", "email": "deen@abc.com", "mobile_no": "9833213601", "password": "hard" }
score:0
Your doing one unnecessary (and erroneous) indirection and your nesting your seializers backwards. Try this instead:
from rest_framework import serializers
from .models import WUser
class BaseUserSerializer(serializers.ModelSerializer):
class Meta:
model = WUser
fields = ('user_type', 'username', 'email', 'first_name', 'last_name', 'password', 'mobile_no')
class UserTypeSerializer(serializers.Serializer):
user_type = serializers.IntegerField(min_value=0, max_value=8, default=0)
user = BaseUserSerializer()
And create the UserTypeSerializer
as the "root" (i e in view).
Credit To: stackoverflow.com
Related Query
- How to pass extra argument in Model Serializer
- How to pass an extra argument to django admin custom form?
- How to pass django model field value as an argument to callable which is default of field in the same model?
- How to pass Extra Context Into Django Rest Serializer from View
- Django Rest Framework: How to pass extra argument to django serializer?
- How to pass arguments to model save method in django through serializer create method
- How to pass concatenated functions as a argument in django model method
- Pass extra arguments to Serializer Class in Django Rest Framework
- django-rest-framework how to make model serializer fields required
- How to pass an argument to a method on a template variable in Django?
- How to pass model fields to a JsonResponse object
- How to pass boolean keyword argument along with the use of "Include" template tag
- How to save extra fields on registration using custom user model in DRF + django-rest-auth
- Django Rest Framework SerializerMethodField Pass Extra Argument
- How to append extra data to the existing serializer in django
- How to pass choice display name to model serialize in Django REST framework?
- how can i pass argument to celery task?
- Django. How to add an extra field in User model and have it displayed in the admin interface
- Django foreign key on_delete: how to pass argument to callable when using models.SET
- Pass extra arguments to nested Serializer in Django Rest Framework
- How can I create model instance via Serializer without creating models from nested serialziers?
- Pass argument to __init__ of object model class in iterate queryset
- How do I pass in a python pandas.Dataframe object as an argument to a celery task?
- How can I pass user id into a serializer with request data?
- How to include extra data in the Django serializer response?
- How do you pass parameters which are not associated with a serializer field in Django Rest Framework?
- Graphene-Django - how to pass an argument from Query to class DjangoObjectType
- how to post multiple model data through one serializer in django rest api
- How to pass argument name as a parameter?
- How to pass extra kwargs to form in django modelformset factory?
More Query from same tag
- How can I use django to call a python script and return the result in realtime?
- Where can I download the Django documentation?
- What is advantage of using serializer to save object in django?
- Django: m2m_changed not fired when end of relation is deleted
- How can I add data to PostgreSQL in real-time chatbot based on Django websockets?
- Having trouble with django.core.exceptions.SuspiciousFileOperation: The joined path
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 1: invalid continuation byte in Django
- Show the main page in the django multi-tenant
- Django objects.get(): The QuerySet value for an exact lookup must be limited to one result using slicing
- How to change the pinax(0.9a2) template?
- How do I host a Django project's admin static files?
- django-rest-framework: serializer from DATA don't update model ID
- Making different registration forms in Django?
- Related posts by tag name
- Django 1.4.2 / South 0.7.6 database error - DEFAULT_DB_ALIAS