Django RestFramework Serializer request.data with array object - python

i have this situation, i received by POST this data:
{'fields':[{'key':'comment', 'value':'something', 'data_type':'string'},
{'key':'days', 'value':'2', 'data_type':'int'}]}
My serializers
class FieldSerializer(serializers.Serializer):
value = serializers.CharField(max_length=200)
data_type = serializers.CharField(max_length=200)
key = serializers.CharField(max_length=200)
class FieldsSerializer(serializers.Serializer):
fields = FieldSerializer(many=True)
In my view pass request.data to FieldsSerializer()
serializer = FieldsSerializer(data=request.data)
serializer.is_valid()
raise Exception(serializer.data, serializer.errors)
output:
Exception: (ReturnDict([('fields', [])]), ReturnDict())
i use versions Django==1.8.15 and djangorestframework==3.0

you has single data for your FieldsSerializer, so you just need to remove many=True while initial the serializer.
serializer = FieldsSerializer(data=request.data)
# ^^^^^^
or as universal solution
many = isinstance(request.data, list)
serializer = FieldsSerializer(data=request.data, many=many)

Related

How to give the data to serializer with JSONField

I have model and serializer with JSONField
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
detail = models.JSONField(default=dict)
def __str__(self):
return self.user.username
class ProfileSerializer(ModelSerializer):
class Meta:
model = m.Profile
fields = '__all__'
Then,I want to set the data to serializer class,
However some how serializer.is_valid() faild.
I have tested two pettern data1 data2
temp_data = {"data":"test"}
data1 = {"detail":temp_data} # test_data1
data2 = {"detail":json.dumps(temp_data)} # test_data2
print(data1) # {'detail': {'data': 'test'}}
print(data2) # {'detail': '{"data": "test"}'}
instance = self.get_object()
serializer = self.get_serializer(instance,data = data1) # or data2
if serializer.is_valid():
# serializer
else:
print("is_valid failed")
What data should I give to this serializer?
And is there any method to debug is_valid()?
You are giving the data in right way, but as you are updating partially, you need to pass one more parameter:
serializer = self.get_serializer(instance, data=data1, partial=True)
To debug the is_valid method, you can either throw exceptions from it like .is_valid(raise_exception=True); or in your code, print serializer.errors in else block to print the exact errors. More information on validation can be found in documentation.

Django Serializer only serializing objects with many=True

I am trying to make a serializer
class StoreSerializer(serializers.ModelSerializer):
class Meta:
model = Store
fields = '__all__'
and in the viewset,
def list(self, request, *args, **kwargs):
obj = Store.objects.first()
ser = StoreSerializer(data=obj)
if ser.is_valid():
pass
print(ser.data)
return Response(ser.data)
this method is returning just an empty dict {} as the response. When defining the serializer as
ser = StoreSerializer(data=[obj], many=True)
the object is getting serialized. What am I doing wrong here?
When you are passing a models data to a ModelSerializer you don't need to call the function
serializer.is_valid()
So you can just pass the data without the data key as
ser = StoreSerializer(obj)
print(ser.data)

how can insert multiple record using

I'm working on a small project Django Rest Framework, I already create add contact function as you can see in my create function. now I'm working on bulk import, but when I submit my data as a list not as a dict I get an error message :
{"non_field_errors":["Invalid data. Expected a dictionary, but got list."]}
this is my code to add a contact,
class ContactView(ListModelMixin, viewsets.GenericViewSet):
queryset = Contact.objects.all()
serializer_class = ContactSerializer
def create(self, request):
serializeObject = ContactSerializer(data = request.data)
if serializeObject.is_valid():
serializeObject.save()
contactObject = Contact.objects.all()
contactSerializer = ContactSerializer(contactObject, many=True)
return Response(contactSerializer.data, status = status.HTTP_201_CREATED)
return Response(serializeObject.errors, status.HTTP_400_BAD_REQUEST)
Now i would like to create another function, for bulk create, since i have a list
This is my header data structure :
[{"Greeting":"amine","first_name":"alain","last_name":"amine","title":"ricardo","language":"ab#xyz.com","email":43822510594,"phone_1":43822510594,"phone_2":"not yet","mobile":43822510594,"fax":"not yet","internal_id":"xname"},{"Greeting":"bill","first_name":"microsoft","last_name":"bill","title":"microsoft","language":"bill#microsoft.com","email":652565455,"phone_1":652565455,"phone_2":"new york","mobile":652565455,"fax":"new york","internal_id":"microsoft"},{"Greeting":"john","first_name":"Yoyo","last_name":"Ruth","title":"xnameagain","language":"rh#xyz.com","email":5465559852,"phone_1":5465559852,"phone_2":"Vancouver","mobile":5465559852,"fax":"Vancouver","internal_id":"yname"}]
This is my serializer:
class ContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = "__all__"
I found the Solution on https://www.django-rest-framework.org/api-guide/serializers/#dealing-with-multiple-objects
all what i have to do is to add many=True to create multiple object
serializeObject = ContactSerializer(data = request.data, many=True)
Create method should look like this:
class ContactView(ListModelMixin, viewsets.GenericViewSet):
queryset = Contact.objects.all()
serializer_class = ContactSerializer
def create(self, request):
valid_objects = []
for data in request.data:
serializeObject = ContactSerializer(data=data)
if serializeObject.is_valid():
valid_objects.append(serializeObject)
else:
return Response(serializeObject.errors, status.HTTP_400_BAD_REQUEST)
for obj in valid_objects:
obj.save()
contactObject = Contact.objects.all()
contactSerializer = ContactSerializer(contactObject, many=True)
return Response(contactSerializer.data, status = status.HTTP_201_CREATED)
Advise
They may not be the best practices but it works.

data in serializer is always empty

I have following serializer:
class AdminSerializer(serializers.Serializer):
def validate(self, data):
user = data.get("user_pk")
total_licenses = data.get("total_licenses")
#here i do some validation with the vars
But my data is always empty. This is part of my view
serializer_class = self.get_serializer_class()
serializer = serializer_class(
data=self.request.data,
)
serializer.is_valid(raise_exception=True)
This is my unit test request:
response = self.client.patch(
url,
data={"user_pk": self.user.pk, "total_licenses": 3},
)
Why is my 'data' always empty?
You have to explicitly specify fields in your serializer:
class AdminSerializer(serializers.Serializer):
user_pk = serializers.IntegerField()
total_licenses = serializers.IntegerField()
def validate(self, data):
...
You need to call serializer.save() before you can access .data attribute on a serializer. Otherwise, you can access .validated_data attribute on your serializer

Django DRF change serializer data with CreateListModelMixin

I have this class view, but I am unable to modify the serializer data to insert more data (which is needed and needs to be populated automatically).
Because I am creating many instances at once, the serializer is based on kwargs['many'] = True.
Any idea on how I can add another field to each serializer data?
Thanks,
:
class ReservedServiceView(CreateListModelMixin, ModelViewSet):
queryset = ReservedService.objects.all()
serializer_class = ReservedServiceSerializer
authentication_classes = (authentication.TokenAuthentication,)
def perform_create(self, serializer):
# Create an event that is a Reflection of the Reserved Service
serializer_data = self.request.data
for reserved_service in serializer_data:
print("--------",reserved_service, flush=True)
service_id = reserved_service['original_service']
original_service = Service.objects.get(pk=service_id)
calendar_event = CalendarEvent()
calendar_event.name = original_service.name
calendar_event.description = original_service.description
calendar_event.date = reserved_service['date']
calendar_event.type_id = 1
calendar_event.start_time = reserved_service['start_time']
calendar_event.end_time = reserved_service['end_time']
calendar_event.location_id = original_service.location.id
calendar_event.save()
reserved_service['associated_event'] = calendar_event.id
print("**********1", serializer_data)
print("**********2", self.request.data)
serializer.save()
Based in:
class CreateListModelMixin(object):
def get_serializer(self, *args, **kwargs):
""" if an array is passed, set serializer to many """
if isinstance(kwargs.get('data', {}), list):
kwargs['many'] = True
return super(CreateListModelMixin, self).get_serializer(*args, **kwargs)
I am not able to properly get your question, but if your question is that you are not getting the extra fields which you added to the serializer in the response of the view, then here is the answer for it.
The response of this view is returned by create method of CreateModelMixin which passes serializer.data to the data param of Response. You cannot update serializer.data because it is an immutable object. So, to solve this you will have to override the create method as follows:
class ReservedServiceView(CreateListModelMixin, ModelViewSet):
queryset = ReservedService.objects.all()
serializer_class = ReservedServiceSerializer
authentication_classes = (authentication.TokenAuthentication,)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
my_data = {}.update(serializer.validated_data)
# Now you can work over the my_data and add extra fields to it and save it
# and instead of passing serializer.data we pass my_data to Response class
headers = self.get_success_headers(serializer.data)
return Response(my_data, status=status.HTTP_201_CREATED, headers=headers)

Categories