How to dynamically remove fields from serializer output

You can customize the serialization behavior by overriding the to_representation() method in your serializer. class DynamicSerliazer(serializers.ModelSerializer): def to_representation(self, obj): # get the original representation ret = super(DynamicSerializer, self).to_representation(obj) # remove ‘url’ field if mobile request if is_mobile_platform(self.context.get(‘request’, None)): ret.pop(‘url’) # here write the logic to check whether `elements` field is to be removed # pop … Read more

AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context

You’re getting this error as the HyperlinkedIdentityField expects to receive request in context of the serializer so it can build absolute URLs. As you are initializing your serializer on the command line, you don’t have access to request and so receive an error. If you need to check your serializer on the command line, you’d … Read more

Dynamically exclude or include a field in Django REST framework serializer

Have you tried this technique class QuestionSerializer(serializers.Serializer): def __init__(self, *args, **kwargs): remove_fields = kwargs.pop(‘remove_fields’, None) super(QuestionSerializer, self).__init__(*args, **kwargs) if remove_fields: # for multiple fields in a list for field_name in remove_fields: self.fields.pop(field_name) class QuestionWithoutTopicView(generics.RetrieveAPIView): serializer_class = QuestionSerializer(remove_fields=[‘field_to_remove1’ ‘field_to_remove2’]) If not, once try it.

How to serialize Django queryset.values() into json?

As other people have said, Django’s serializers can’t handle a ValuesQuerySet. However, you can serialize by using a standard json.dumps() and transforming your ValuesQuerySet to a list by using list(). If your set includes Django fields such as Decimals, you will need to pass in DjangoJSONEncoder. Thus: import json from django.core.serializers.json import DjangoJSONEncoder queryset = … Read more

How to display all model fields with ModelSerializer?

According to the Django REST Framework’s Documentation on ModelSerializers: By default, all the model fields on the class will be mapped to a corresponding serializer fields. This is different than Django’s ModelForms, which requires you to specify the special attribute ‘__all__’ to utilize all model fields. Therefore, all that is necessary is to declare the … Read more

Editing django-rest-framework serializer object before save

Now edited for REST framework 3 With REST framework 3 the pattern is now: if serializer.is_valid(): serializer.save(user_id=15) Note that the serializers do not now ever expose an unsaved object instance as serializer.object, however you can inspect the raw validated data as serializer.validated_data. If you’re using the generic views and you want to modify the save … Read more

Django REST framework serializer without a model

You can create a serializer that inherits from serializers.Serializer and pass your data as the first parameter like: serializers.py from rest_framework import serializers class YourSerializer(serializers.Serializer): “””Your data serializer, define your fields here.””” comments = serializers.IntegerField() likes = serializers.IntegerField() views.py from rest_framework import views from rest_framework.response import Response from .serializers import YourSerializer class YourView(views.APIView): def get(self, … Read more

Django rest framework serializing many to many field

You will need a TagSerializer, whose class Meta has model = Tag. After TagSerializer is created, modify the PostSerializer with many=True for a ManyToManyField relation: class PostSerializer(serializers.ModelSerializer): tag = TagSerializer(read_only=True, many=True) class Meta: model = Post fields = (‘tag’, ‘text’,) Answer is for DRF 3

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)