DRF: Pass serializer depth with a GET parameter - python

It's possible to change a serializer depth with a GET parameter? For example calling http://localhost:8000/api-auth/?depth=1

The proper way to pass some extra data from view to serializer is the serializer context.
In DRF class-based view, you can (and, actually, should for such purposes) override get_serializer_context() method. In the overridden method you just add to the context, which is just a dictionary, whatever you want.
Simple example, how to do this in view:
class YourView(generics.RetrieveAPIView):
def get_serializer_context(self):
context = super().get_serializer_context()
context['depth'] = self.request.query_params.get('depth', 1)
return context
And the to access it in serializer:
class YourSerializer(serializers.ModelSerializer):
the_depth_from_get_param = serializers.SerializerMethodField()
class Meta:
model = YourModel
fields = [
'the_depth_from_get_param'
]
def get_the_depth_from_get_param(self, obj):
return self.context['depth']

I've solved it in a really simple way, assuming that views are made with class based views or viewsets:
The serializer_class propriety it's actually the serializer, it's a class, so you can access to the depth value with serializer_class.Meta.depth, and assign it a value from the self.request.GET array
Example serializer:
class ItemSerializer(ModelSerializer):
class Meta:
model = Item
fields = '__all__'
Example view:
class ItemList(generics.ListCreateAPIView):
queryset = Item.objects.all()
serializer_class = serializers.ItemSerializer
def get_serializer_class(self):
if(self.request.GET.get('depth')):
if (10 >= int(self.request.GET.get('depth')) >= 0):
self.serializer_class.Meta.depth = int(
self.request.GET.get('depth'))
return self.serializer_class

Related

Django Rest Framework fail on setting a new context to the serializer

Django time:
I am facing an issue with providing a context to the serializer:
class CommentSerializer(serializers.ModelSerializer):
likes = CustomUserSerializer(many=True,source='likes.all')
class Meta:
fields = 'likes',
model = models.Comment
def get_user_like(self,obj):
for i in obj.likes.all():
if self.context['user'] in i.values():
return self.context['user']
in the view:
class CommentView(viewsets.ModelViewSet):
serializer_class = serializer.CommentSerializer
def get_serializer_context(self): #adding request.user as an extra context
context = super(CommentView,self).get_serializer_context()
context.update({'user':self.request.user})
return context
as you can see, i have overridded get_serializer_context to add user as a context
however, in the serializer side, i am getting KeyError:'user' means the key does not exist, any idea how to set a context?
This is not necessary and inefficient. You can just annotate with:
from django.db.models import Exists, OuterRef
class CommentView(viewsets.ModelViewSet):
serializer_class = serializer.CommentSerializer
def get_queryset(self):
return Comment.objects.annotate(
user_like=Exists(
Like.objects.filter(
comment_id=OuterRef('pk'), user_id=self.request.user.pk
)
)
).prefetch_related('likes')
In the serializer we then add the user_like field:
class CommentSerializer(serializers.ModelSerializer):
likes = CustomUserSerializer(many=True)
user_like = serializers.BooleanField(read_only=True)
class Meta:
fields = ('likes',)
model = models.Comment

Django Rest Framework Calculations in Serializer?

I'm working with a finance application, and due to the way that floating point math works, have decided to store all values in the database as cents (so dollar amount * 100). I have been banging my head against a wall to get the serializer to perform two calculations for me. On create/update accept a float value but then before saving to the database do value*100. Then on get, do value/100.
I got it half working using a SerializerMethodField, but that seemed to remove my ability to do create/update actions. I also at one point had something that kind of worked for create/update by changing the serializer.save() method in the view and adding an IntegerField validator on the field, but then that broke the SerializerMethodField.
In short, I'm stuck. lol
Here is my very simple model:
class Items(models.Model):
user = models.ForeignKey(
'CustomUser',
on_delete=models.CASCADE,
)
name = models.CharField(max_length=60)
total = models.IntegerField()
My views for this item:
class GetItems(generics.ListCreateAPIView):
serializer_class = ItemsSerializer
permission_classes = [permissions.IsAuthenticated, IsAuthorOrDenied]
user = serializers.HiddenField(default=serializers.CurrentUserDefault(), )
def get_queryset(self):
user = self.request.user
return Items.objects.filter(user=user)
def perform_create(self, serializer):
serializer.save(user=self.request.user)
class SingleItem(generics.RetrieveUpdateDestroyAPIView):
serializer_class = ItemsSerializer
permission_classes = [permissions.IsAuthenticated, IsAuthorOrDenied]
user = serializers.HiddenField(default=serializers.CurrentUserDefault(), )
def get_queryset(self):
user = self.request.user
return Items.objects.filter(user=user)
def perform_update(self, serializer):
serializer.save(user=self.request.user)
And my serializer
class ItemsSerializer(serializers.ModelSerializer):
class Meta:
fields = ('id', 'name', 'budget_total')
model = models.Items
I feel like I should be doing more in my Serializer and less in my views, but that may be a completely different question all together.
Thanks in advance for the help!
Custom Serializer Field
You could write a custom field to handle the data:
class BudgetField(serializers.Field):
def to_representation(self, value):
# You can decide here how you want to return your data back
return value / 100
def to_internal_value(self, data):
# this will be passed to validated_data, so will be used to create/update instances
# you could do some validation here to make sure it is a float
# https://www.django-rest-framework.org/api-guide/fields/#raising-validation-errors
return int(data * 100)
Then use the custom field on your serializer.
class ItemsSerializer(serializers.ModelSerializer):
total = BudgetField()
class Meta:
fields = ('id', 'name', 'total')
model = models.Items
Override .update() and .create()
You could also choose to override these methods on the serializer.
class ItemsSerializer(serializers.ModelSerializer):
class Meta:
fields = ('id', 'name', 'budget_total')
model = models.Items
def create(self, validated_data):
# Modify validated_data with the value you need
return super().create(validated_data)
def update(self, instance, validated_data):
# Modify validated_data with the value you need
return super().update(instance, validated_data)

How to sort values of serializer custom field in DRF

I have created a custom field in the client serializer.
The value of this field is calculated by a complex serializer method.
class ClientsStatsSerializer(serializers.ModelSerializer):
"""
Serializer shows total_spend for 2019 by client.
"""
refs_count = serializers.SerializerMethodField()
total_spend_2019 = serializers.SerializerMethodField()
class Meta:
model = Company
ordering = ('total_spend_2019',)
fields = [
'id',
'legal_name',
'refs_count',
'total_spend_2019',
]
def get_total_spend_2019(self, obj):
...
I would like to get the output sorted by the value of total_spend_2019. It looks like I cannot do it here with a simple ordering = ('total_spend_2019',) I cannot do it either in the model, neither in the view.
EDIT: It would be great to have a generic solution that would work with any SerializerMethodField.
Current view is as such:
class ClientsStatsViewSet(viewsets.ViewSet):
def list(self, request):
queryset = request.user.company.clients.all()
client_id = self.request.query_params.get('client_id', None)
if client_id is not None:
queryset = queryset.filter(pk=client_id)
serializer = ClientsStatsSerializer(queryset, many=True)
return Response(serializer.data)
Any idea how to solve this?
Thank you very much!!
All right, I managed to solve it with the following:
class ClientsStatsViewSet(viewsets.ViewSet):
def list(self, request):
queryset = request.user.company.clients.all()
client_id = self.request.query_params.get('client_id', None)
if client_id is not None:
queryset = queryset.filter(pk=client_id)
serializer = ClientsStatsSerializer(queryset, many=True)
serializer_data = sorted(
serializer.data, key=lambda k: k['total_spend_2019'], reverse=True)
return Response(serializer_data)
I don't know if it is the most performant way to do this, but it works.
Also you can try create your custom model manager. This is popular decision for ordering data:
models.py
class CustomManager(models.Manager):
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(...).order_by(...)
class Company(models.Model):
...
objects = models.Manager
custom_manager = CustomManager()
views.py
class CompanyViewSet(viewsets.ViewSet):
...
queryset = Company.custom_manager.all()
...

How to make a field editable=False in DRF

I've a serializer. I want to restrict updating a field. How would I do that?
class ABCSerializer(serializers.ModelSerializer):
class Meta:
"""Meta."""
model = ModelA
fields = ('colA', 'colB', 'colC',)
colA is a required field while creating the object. However, it should not be allowed to update. How can I do that??
Sounds like you need different serializers for PUT and POST methods. In the serializer for the PUT method you can set the colA field to readonly
class ABCViewSet(ModelViewSet):
serializer_class = ABCSerializer
def get_serializer_class(self):
serializer_class = self.serializer_class
if self.request.method == 'PUT':
serializer_class = SerializerWithReadOnlyColA
return serializer_class
You can use Django REST Frameworks field-level validation by validating that field has not changed on update like so:
from rest_framework.exceptions import ValidationError
class ABCSerializer(serializers.ModelSerializer):
colA = serializers.CharField(max_length=100)
def validate_colA(self, value):
if self.instance and self.instance.colA != value:
raise ValidationError("You may not edit colA")
return value
class Meta:
"""Meta."""
model = ModelA
fields = ('colA', 'colB', 'colC',)
This will check whether or not this is an update (via checking if an instance is populated on the serializer) and if so it will then check to see if you have made a change to the field and if you have it will throw a ValidationError. The benefit of this approach is that you can keep your view code the same as before and continue to keep your validation behaviour in your serializer.
You can override the serializer's update method to only update fields that you want.
class ABCSerializer(serializers.ModelSerializer):
def update(self, instance, validated_data):
instance.colB = validated_data.get('colB', instance.colB)
instance.colC = validated_data.get('colC', instance.colC)
# do nothing to instance.colA
instance.save()
return instance
class Meta:
model = ModelA
fields = ('colA', 'colB', 'colC',)
Or if you have many fields, and just want to omit updating colA, you could write your update method like this:
def update(self, instance, validated_data):
validated_data.pop('colA') # validated_data no longer has colA
return super().update(instance, validated_data)
You can read more about overriding update here: https://www.django-rest-framework.org/api-guide/serializers/#saving-instances
I think it's too late to answer but this may be useful for others:)
you can solve your problem this way:
class ABCSerializer(serializers.ModelSerializer):
class Meta:
model = ModelA
fields = ('colA', 'colB', 'colC',)
def get_fields(self):
fields = super().get_fields()
if self.instance:
fields["colA"].read_only = True
return fields
When you want to create, the self.instance is None, it will pass the if clause, and in case of updating the if clause will make the field read only and non-editable.
You can do this with the read_only_fieldsoption
class ABCSerializer(serializers.ModelSerializer):
class Meta:
"""Meta."""
model = ModelA
fields = ('colB', 'colC',)
read_only_fields = ('colA',)

django rest framework hide specific fields in list display?

I want to hide specific fields of a model on the list display at persons/ and show all the fields on the detail display persons/jane
I am relatively new to the rest framework and the documentation feels like so hard to grasp.
Here's what I am trying to accomplish.
I have a simple Person model,
# model
class Person(models.Model):
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
nickname = models.CharField(max_length=20)
slug = models.SlugField()
address = models.TextField(max_length=300, blank=True)
and the serializer class
# serializers
class PersonListSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = ('nickname', 'slug')
class PersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = ('first_name', 'last_name', 'nickname', 'slug', 'address')
and the viewsets.
# view sets (api.py)
class PersonListViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonListSerializer
class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
at the url persons I want to dispaly list of persons, just with fields nickname and slug and at the url persons/[slug] I want to display all the fields of the model.
my router configurations,
router = routers.DefaultRouter()
router.register(r'persons', api.PersonListViewSet)
router.register(r'persons/{slug}', api.PersonViewSet)
I guess the second configuration is wrong, How can I achieve what I am trying to do?
update:
the output to persons/slug is {"detail":"Not found."} but it works for person/pk
Thank you
For anyone else stumbling across this, I found overriding get_serializer_class on the viewset and defining a serializer per action was the DRY-est option (keeping a single viewset but allowing for dynamic serializer choice):
class MyViewset(viewsets.ModelViewSet):
serializer_class = serializers.ListSerializer
permission_classes = [permissions.IsAdminUser]
renderer_classes = (renderers.AdminRenderer,)
queryset = models.MyModel.objects.all().order_by('-updated')
def __init__(self, *args, **kwargs):
super(MyViewset, self).__init__(*args, **kwargs)
self.serializer_action_classes = {
'list':serializers.AdminListSerializer,
'create':serializers.AdminCreateSerializer,
'retrieve':serializers.AdminRetrieveSerializer,
'update':serializers.AdminUpdateSerializer,
'partial_update':serializers.AdminUpdateSerializer,
'destroy':serializers.AdminRetrieveSerializer,
}
def get_serializer_class(self, *args, **kwargs):
"""Instantiate the list of serializers per action from class attribute (must be defined)."""
kwargs['partial'] = True
try:
return self.serializer_action_classes[self.action]
except (KeyError, AttributeError):
return super(MyViewset, self).get_serializer_class()
Hope this helps someone else.
You can override the 'get_fields' method your serializer class and to add something like that:
def get_fields(self, *args, **kwargs):
fields = super().get_fields(*args, **kwargs)
request = self.context.get('request')
if request is not None and not request.parser_context.get('kwargs'):
fields.pop('your_field', None)
return fields
In this case when you get detail-view there is 'kwargs': {'pk': 404} and when you get list-view there is 'kwargs': {}
I wrote an extension called drf-action-serializer (pypi) that adds a serializer called ModelActionSerializer that allows you to define fields/exclude/extra_kwargs on a per-action basis (while still having the normal fields/exclude/extra_kwargs to fall back on).
The implementation is nice because you don't have to override your ViewSet get_serializer method because you're only using a single serializer. The relevant change is that in the get_fields and get_extra_kwargs methods of the serializer, it inspects the view action and if that action is present in the Meta.action_fields dictionary, then it uses that configuration rather than the Meta.fields property.
In your example, you would do this:
from action_serializer import ModelActionSerializer
class PersonSerializer(ModelActionSerializer):
class Meta:
model = Person
fields = ('first_name', 'last_name', 'nickname', 'slug', 'address')
action_fields = {
'list': {'fields': ('nickname', 'slug')}
}
Your ViewSet would look something like:
class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
And your router would look normal, too:
router = routers.DefaultRouter()
router.register(r'persons', api.PersonViewSet)
Implementation
If you're curious how I implemented this:
I added a helper method called get_action_config which gets the current view action and returns that entry in the action_fields dict:
def get_action_config(self):
"""
Return the configuration in the `Meta.action_fields` dictionary for this
view's action.
"""
view = getattr(self, 'context', {}).get('view', None)
action = getattr(view, 'action', None)
action_fields = getattr(self.Meta, 'action_fields', {})
I changed get_field_names of ModelSerializer:
From:
fields = getattr(self.Meta, 'fields', None)
exclude = getattr(self.Meta, 'exclude', None)
To:
action_config = self.get_action_config()
if action_config:
fields = action_config.get('fields', None)
exclude = action_config.get('exclude', None)
else:
fields = getattr(self.Meta, 'fields', None)
exclude = getattr(self.Meta, 'exclude', None)
Finally, I changed the get_extra_kwargs method:
From:
extra_kwargs = copy.deepcopy(getattr(self.Meta, 'extra_kwargs', {}))
To:
action_config = self.get_action_config()
if action_config:
extra_kwargs = copy.deepcopy(action_config.get('extra_kwargs', {}))
else:
extra_kwargs = copy.deepcopy(getattr(self.Meta, 'extra_kwargs', {}))
If you want to change what fields are displayed in the List vs Detail view, the only thing you can do is change the Serializer used. There's no field that I know of that lets you specify which fields of the Serializer gets used.
The field selection on you serializers should be working, but I don't know what might be happening exactly. I have two solutions you can try:
1 Try to change the way you declare you serializer object
#If you aren't using Response:
from rest_framework.response import Response
class PersonListViewSet(viewsets.ModelViewSet):
def get(self, request):
queryset = Person.objects.all()
serializer_class = PersonListSerializer(queryset, many=True) #It may change the things
return Response(serializer_class.data)
class PersonViewSet(viewsets.ModelViewSet):
def get(self, request, pk): #specify the method is cool
queryset = Person.objects.all()
serializer_class = PersonSerializer(queryset, many=True) #Here as well
#return Response(serializer_class.data)
2 The second way around would change your serializers
This is not the most normal way, since the field selector should be working but you can try:
class PersonListSerializer(serializers.ModelSerializer):
nickname = serializers.SerializerMethodField() #Will get the attribute my the var name
slug = serializers.SerializerMethodField()
class Meta:
model = Person
def get_nickname(self, person):
#This kind of method should be like get_<fieldYouWantToGet>()
return person.nickname
def get_slug(self, person):
#This kind of method should be like get_<fieldYouWantToGet>()
return person.slug
I hope it helps. Try to see the APIview class for building your view too.
Somehow close:
If you just want to skip fields in the serilaizer
class UserSerializer(serializers.ModelSerializer):
user_messages = serializers.SerializerMethodField()
def get_user_messages(self, obj):
if self.context.get('request').user != obj:
# do somthing here check any value from the request:
# skip others msg
return
# continue with your code
return SystemMessageController.objects.filter(user=obj, read=False)
I rewrite ModelViewSet list function to modify serializer_class.Meta.fields attribute, code like this:
class ArticleBaseViewSet(BaseViewSet):
def list(self, request, *args, **kwargs):
exclude = ["content"]
self.serializer_class.Meta.fields = [f.name for f in self.serializer_class.Meta.model._meta.fields if f.name not in exclude]
queryset = self.filter_queryset(self.get_queryset()).filter(is_show=True, is_check=True)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
class BannerArticleViewSet(ArticleBaseViewSet):
queryset = BannerArticle.objects.filter(is_show=True, is_check=True).all()
serializer_class = BannerArticleSerializer
permission_classes = (permissions.AllowAny,)
But it looks not stable, so i will not use it, just share to figure out the best way
My solution.
class BaseSerializerMixin(_ModelSerializer):
class Meta:
exclude: tuple[str, ...] = ()
exclude_in_list: tuple[str, ...] = ()
model: Type[_models.Model]
def get_action(self) -> Optional[str]:
if 'request' not in self.context:
return None
return self.context['request'].parser_context['view'].action
def get_fields(self):
fields = super().get_fields()
if self.get_action() == 'list':
[fields.pop(i) for i in list(fields) if i in self.Meta.exclude_in_list]
return fields
I think it should be like this:
router.register(r'persons/?P<slug>/', api.PersonViewSet)
and you should include a line like this:
lookup_field='slug'
in your serializer class. Like this:
class PersonSerializer(serializers.ModelSerializer):
lookup_field='slug'
class Meta:
model = Person
fields = ('first_name', 'last_name', 'nickname', 'slug', 'address')

Categories

Resources