Multiple lookup_fields for django rest framework

Try this from django.db.models import Q import operator from functools import reduce from django.shortcuts import get_object_or_404 class MultipleFieldLookupMixin(object): def get_object(self): queryset = self.get_queryset() # Get the base queryset queryset = self.filter_queryset(queryset) # Apply any filter backends filter = {} for field in self.lookup_fields: filter[field] = self.kwargs[field] q = reduce(operator.or_, (Q(x) for x in filter.items())) return … Read more

context in nested serializers django rest framework

Ok i found a working solution. I replaced the ChildSerializer assignment in the Parent class with a SerializerMethodField which adds the context. This is then passed to the get_fields method in my CustomModelSerializer: class ChildSerializer(CustomModelSerializer): class Meta: fields = (‘c_name’, ) model = Child class ParentSerializer(CustomModelSerializer): child = serializers.SerializerMethodField(‘get_child_serializer’) class Meta: model = Parent fields … Read more

Django Rest framework, how to include ‘__all__’ fields and a related field in ModelSerializer ?

Like @DanEEStart said, DjangoRestFramework don’t have a simple way to extend the ‘all‘ value for fields, because the get_field_names methods seems to be designed to work that way. But fortunately you can override this method to allow a simple way to include all fields and relations without enumerate a tons of fields. I override this … Read more

How can I test binary file uploading with django-rest-framework’s test client?

When testing file uploads, you should pass the stream object into the request, not the data. This was pointed out in the comments by @arocks Pass { ‘image’: file} instead But that didn’t full explain why it was needed (and also didn’t match the question). For this specific question, you should be doing from PIL … Read more

CSRF validation does not work on Django using HTTPS

Django 4.0 and above For Django 4.0 and above, CSRF_TRUSTED_ORIGINS must include scheme and host, e.g.: CSRF_TRUSTED_ORIGINS = [‘https://front.bluemix.net’] Django 3.2 and lower For Django 3.2 and lower, CSRF_TRUSTED_ORIGINS must contain only the hostname, without a scheme: CSRF_TRUSTED_ORIGINS = [‘front.bluemix.net’] You probably also need to put something in ALLOWED_HOSTS…

How to pass extra context to Django Rest Framework serializers

You have to use get_object as func, not as property: class PostViewSets(viewsets.ModelViewSet): queryset = Post.objects.all() serializer_class = PostSerializer def get_serializer_context(self): context = super(PostViewSets, self).get_serializer_context() author = self.get_object().author author_posts = self.get_queryset().filter(author=author) context.update({‘author_posts’: author_posts}) return context

Django: app level variables

A possible solution is to implement a custom Django middleware, as described in https://docs.djangoproject.com/ja/1.9/topics/http/middleware/. You could initialize the HTTP connection pool in the middleware’s __init__ method, which is only called once at the first request. Then, start the HTTP request during process_request and on process_response check it has finished (or wait for it) and append … Read more

Override JSONSerializer on django rest framework

I think you have your answer there in the post you’ve given. You need to define custom JSON renderer from rest_framework.renderers import JSONRenderer class EmberJSONRenderer(JSONRenderer): def render(self, data, accepted_media_type=None, renderer_context=None): data = {‘element’: data} return super(EmberJSONRenderer, self).render(data, accepted_media_type, renderer_context) and use it as default renderer either in settings or as an explicitly defined render for … Read more