Django REST Framework: raise error when extra fields are present on POST

Came across this question and found that using object level validation is a bit easier. This entails simply defining a validate method: class ModelASerializer(serializers.ModelSerializer): … def validate(self, data): if hasattr(self, ‘initial_data’): unknown_keys = set(self.initial_data.keys()) – set(self.fields.keys()) if unknown_keys: raise ValidationError(“Got unknown fields: {}”.format(unknown_keys)) return data

Django REST Serializer Method Writable Field

Here is a read/write serializer method field: class ReadWriteSerializerMethodField(serializers.SerializerMethodField): def __init__(self, method_name=None, **kwargs): self.method_name = method_name kwargs[‘source’] = ‘*’ super(serializers.SerializerMethodField, self).__init__(**kwargs) def to_internal_value(self, data): return {self.field_name: data}

How to return generated file download with Django REST Framework?

Here’s an example of returning a file download directly from DRF. The trick is to use a custom renderer so you can return a Response directly from the view: from django.http import FileResponse from rest_framework import viewsets, renderers from rest_framework.decorators import action class PassthroughRenderer(renderers.BaseRenderer): “”” Return data as-is. View should supply a Response. “”” media_type=”” … Read more

How do I set different Serializer for list and detail view with Django Rest Framework?

I’ve adapted an answer from “Django rest framework, use different serializers in the same ModelViewSet” that serves me very well, and I hope you’ll find useful: class MyModelViewSet(viewsets.MyModelViewSet): queryset = MyModel.objects.all() serializer_class = MyModelListSerializer detail_serializer_class = MyModelDetailSerializer def get_serializer_class(self): if self.action == ‘retrieve’: if hasattr(self, ‘detail_serializer_class’): return self.detail_serializer_class return super(MyModelViewSet, self).get_serializer_class() In this case, you’re … Read more

Django REST framework: type object X has no attribute ‘get_extra_actions’

Basically you are trying to register a non-viewset(here InstanceList a view, not a viewset) to router. Instead of this, you can simply use it in urls like this: router = routers.DefaultRouter() router.register(r’users’,views.UserViewSet) urlpatterns = [ path(”, views.dash, name=”dash”), path(‘api/’, include(router.urls)), path(‘api/instances/’, views.InstanceList.as_view(), name=”instances”), ]

Django Rest Framework Nested Serializer required=False error

Ok, so Kevin Browns comment is correct. I needed to add allow_null=True. class SerializerA(serializers.Serializer): details = DetailsSerializer(required=False, allow_null=True) The reason for this is that having required=False allows the field details to be absent from the data when constructing the serializer. e.g. s = SerializerA(data={}) whereas allow_null permits the the param to be specified but be … Read more

How can I define a list field in django rest framework?

There is also the ListField in django rest framework, see http://www.django-rest-framework.org/api-guide/fields/#listfield wtih the examples: scores = serializers.ListField( child=serializers.IntegerField(min_value=0, max_value=100) ) and (other notation): class StringListField(serializers.ListField): child = serializers.CharField() this seems simpler (maybe this class is added later than the accepted anwser, but seems good to add this option anyway) By the way, it’s added since … Read more

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