How to add arbitrary data to validated_data of django rest serializer object? - django-rest-framework

I want to add additional field-value pairs to the serializer's validated data object after the is_valid() operations are done. How can I do that?

You can override the serializer's create-method:
def create(self, validated_data):
"""Add more data to the already validated data and create the object."""
my_new_instance = MyClass.objects.create(
**validated_data, more_data=1337, created_by=self.context["request"].user
)
return my_new_instance

Related

how to get validated data from serializers to views in django rest framework

I am returning some data from def create method in seriaizers.py file I want to acccess that data in views.py I dont know how to get it.
serilizers.py
def create(self, validated_data):
//returning something
return validated_data
views.py
if serializer.is_valid():
serializer.save()
//I want to return that validated_data here so how to get it here here
Response(validated_data)
To get the validated data, use serializer.validated_data. See the Deserializing Objects section of the manual.
Also, I want to point out a potential footgun here: you're supposed to return a model instance, from the create() method, rather than the validated data.

Django: customizing the field types needed for create and retrieve serializers

I currently have the following serializer:
serializers.py
class SurfGroupSerializer(serializers.ModelSerializer):
instructor = SurfInstructorSerializer(many=False)
surfers = SurferSerializer(many=True)
class Meta:
model = SurfGroup
fields = ['uuid', 'instructor', 'date', 'starting_time', 'ending_time', 'surfers']
def create(self, validated_data):
return SurfGroup(**validated_data)
And the following viewset create method (viewset inherited from viewsets.ViewSet as we need some bespoke customization, extra signals and actions etc):
viewsets.py
# Surf Group Create View:
def create(self, request, format=None):
serializer = SurfGroupSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
response = responses.standardized_json_response(
message='Surf Group Objects Have Been Successfully Created',
data=serializer.data
)
return Response(data=response, status=status.HTTP_201_CREATED, headers=headers)
For the retrieve action, the serializer works well, and we have a nested instructor object in the response. However, I want to perform a create by passing in the instructor uuid attrbiute like (see content in the POST textarea):
Rather than a whole object...I was wondering how we achieve this? Is it best to just have two Serializers, one for performing the create, and one the retrieval?
def create(self, validated_data):
surf_group = SurfGroup(
instructor__uuid=validated_data['instructor'],
)
surf_group.save()
return surf_group
It is good question.
I work with this situations many times and it looks like one option is to have two serializers as you mean: 1 for list/retrieve and 1 for save.
Another option (for me) is to set serializer field input as UUID and output as another serializer data like this:
class SurfGroupSerializer(serializers.ModelSerializer):
instructor = serializers.UUIDField()
surfers = SurferSerializer(many=True, read_only=True)
class Meta:
model = SurfGroup
fields = ['uuid', 'instructor', 'date', 'starting_time', 'ending_time', 'surfers']
# I use this validate method to transform uuid to object which will
# be bypassed to create method for easly save
def validate_instructor(self, instructor_uuid):
try:
return Instructor.objects.get(uuid=instructor_uuid)
except Instructor.DoesNotExist:
# Remember that you dont need to pass field_key: [errors] to ValidationError
# because validate_FIELD will automatically pass field_key for you !
raise ValidationError(['Instructor with the given uuid does not exist.'])
# Overwrite output data
def to_representation(self, instance):
ret = super().to_representation(instance)
ret['instructor'] = SurfInstructorSerializer(instance=instance.instructor).data
return ret

Django REST serializer required field

I have a simple serializer with one required field:
class MySerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = '__all__'
read_only_fields = ('field1', 'field2')
In my model there is an 'url' field which is required to create new object (method: POST). I would like to set required: False for PUT method. How can I achieve that? Thanks for any clues...
I assume you want to change/set one or multiple fields of an existing MyModel instance.
In such case, you need to pass a partial=True keyword argument to serializer. Then even if you PUT or PATCH without url field in data, your serializer.is_valid() would evaluate to True.
https://www.agiliq.com/blog/2019/04/drf-polls/#edit-a-poll-question should help if my assumption about your question is correct.
I found this answer helpful: Django Rest Framework set a field read_only after record is created .
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance is not None:
self.fields.get('url').read_only = True
This code works fine.

Serializing inside method field in Django REST framework

For example, I have two models: Model1 and Model2. They are not related directly to each-other by any key-field on a model level. For both models I have serializers. I am searching the way to have Model2 queryset in Model1 serializer. For example:
GET /api/model1/01
According to Model1 ID in request I can make query for Model2 objects that I need to be sent in response. For now I have solution that I don't like: in Model1 serializer I have method field that returns a list of objects. Is there any way to use Model2 serializer in method field of Serializer1 or any other solution for my case?
Solution-1: Using Model2Serializer in a Model1's SerializerMethodField()
In this method, we define a model2_data SerializerMethodField() field in the Model1Serializer. There, we will first fetch all the Model2 objects using the current Model1 object. Then we initialize the Model2Serializer with many=True argument and pass all the obtained Model2 instances. To return the serialized representation of Model2 objects, we access the .data property.
class Model1Serializer(serializers.ModelSerializer):
model2_data = serializers.SerializerMethodField() # define separate field
class Meta:
model = Model1
fields = [.., 'model2_data']
def get_model2_data(self, obj):
# here write the logic to get the 'Model2' objects using 'obj'
# initialize the 'Model2Serializer'
model2_serializer = Model2Serializer(model2_objs, many=True)
# return the serialized representation of 'Model2' objs
return model2_serializer.data
Solution-2: Overriding the retrieve method
Another option is to override the retrieve method and add the model2_data to your response along with original response.
class MyView(generics.RetrieveAPIView):
serializer_class = Model1Serializer
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
# get the original serialized data
serialized_data = serializer.data
# get the 'Model2' objects using 'serializer.instance'
model2_serializer = Model2Serializer(model2_objs, many=True)
model2_data = model2_serializer.data
# add the serialized representation of `Model2` objs
serialized_data['model2_data'] = model2_data
return Response(serialized_data)
PS: I know these solutions are not clean. Had the two models been related, we could have approached the problem in a more cleaner way.

How to programmatically provide `queryset` to PrimaryKeyRelatedField in DRF 3

In order to have a non-readonly PrimaryKeyRelatedField, you are required to provide a queryset that contains valid options.
How can I properly populate that queryset based on the current request (user)?
The key is to subclass PrimaryKeyRelatedField and overload the get_queryset method, using the user information from the request context:
class UserFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
request = self.context.get('request', None)
queryset = super(UserFilteredPrimaryKeyRelatedField, self).get_queryset()
if not request or not queryset:
return None
return queryset.filter(user=request.user)
You can then use this new serializer just like the (unfiltered) original:
class MySerializer(serializers.ModelSerializer):
related = UserFilteredPrimaryKeyRelatedField(queryset=MyModel.objects)
Whenever the serializer accesses the queryset, it will be filtered such that only objects owned by the current user are returned.
View has a
self.request.user
attribute which you can then use to fetch user related queryset
eg
queryset = Products.objects.get(customer=self.request.user)

Resources