Django REST Framework: adding additional field to ModelSerializer

I think SerializerMethodField is what you’re looking for: class FooSerializer(serializers.ModelSerializer): my_field = serializers.SerializerMethodField(‘is_named_bar’) def is_named_bar(self, foo): return foo.name == “bar” class Meta: model = Foo fields = (‘id’, ‘name’, ‘my_field’) http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

How to get Request.User in Django-Rest-Framework serializer?

You cannot access the request.user directly. You need to access the request object, and then fetch the user attribute. Like this: user = self.context[‘request’].user Or to be more safe, user = None request = self.context.get(“request”) if request and hasattr(request, “user”): user = request.user More on extra context can be read here

Django rest framework, use different serializers in the same ModelViewSet

Override your get_serializer_class method. This method is used in your model mixins to retrieve the proper Serializer class. Note that there is also a get_serializer method which returns an instance of the correct Serializer class DualSerializerViewSet(viewsets.ModelViewSet): def get_serializer_class(self): if self.action == ‘list’: return serializers.ListaGruppi if self.action == ‘retrieve’: return serializers.DettaglioGruppi return serializers.Default # I dont’ … Read more

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