How to give the data to serializer with JSONField - python

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.

Related

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

How to update multiple records at once (bulk update) in django API

I need to update categories in many Article in one request.
In ArticleViewSet I have:
def get_serializer_class(self):
if self.action in ['partial_update', 'update']:
return ArticlePostSerializer
return ArticleSerializer
So ArticlePostSerializer need to be changed.
This is my serializers code:
class ArticleShortCategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = 'id', 'name'
class ArticleSerializer(serializers.ModelSerializer):
categories = serializers.SerializerMethodField()
def get_categories(self, obj):
return ArticleShortCategorySerializer(obj.categories, many=True).data
class Meta:
model = Article
read_only_fields = 'id'
fields = ('categories', 'text') + read_only_fields
class ArticlePostSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = 'id', 'categories', 'text'
I tried to add:
class ArticlePostListSerializer(serializers.ListSerializer):
and
class Meta:
list_serializer_class = ArticlePostListSerializer
But it doen't work.
How to change this code to do multiple update.
My json request
{
[id: 90, categories: [10,12,14]],
[id: 93, categories: [10,12,14]],
[id: 95, categories: [10,12,14]]
}
Here is sample of CreateMixins OR UpdateMixins you requested.
======================= VIEW ================================
class OrderCreate(mixins.CreateModelMixin,viewsets.GenericViewSet):
pagination_class = None
def get_queryset(self):
return []
def get_serializer_class(self):
return serializers.OrderSerializer
======================= Serializer =============================
class OrderDetailSerializer(serializers.ModelSerializer):
class Meta:
model = crm_models.OrderDetail
fields = (
'product',
'quantity',
'rate_per_unit',
'order_quantity'
)
class OrderSerializer(serializers.ModelSerializer):
order_details = OrderDetailSerializer(many = True)
class Meta:
model = crm_models.OrderMaster
fields = (
'order',
'invoice_number',
'client',
'beat_check',
'target_customer',
'order_editor_client_employee',
'order_marked',
'order_saved',
'edit_marked',
'edit_saved',
'adhoc',
'order_details'
)
def create(self, validated_data,*args,**kwargs):
ordersdetails_data = validated_data.pop('order_details')
user = None
request = self.context.get("request")
if request and hasattr(request, "user"):
user = request.user
validated_data['client'] = user.client
validated_data['order_editor_client_employee'] = user
validated_data['adhoc'] = validated_data['adhoc'] if 'adhoc' in validated_data else False
orderObj = super(OrderSerializer, self).create(validated_data,*args,**kwargs)
orderdetails = []
for details in ordersdetails_data:
orderdetails.append(crm_models.OrderDetail(
product= details['product'],
quantity = details['quantity'],
rate_per_unit = details['rate_per_unit'],
order_quantity = details['order_quantity'],
order = orderObj
))
crm_models.OrderDetail.objects.bulk_create(orderdetails)
return orderObj
In Update view function name would be changed to update, you can find more documentation http://www.django-rest-framework.org/api-guide/generic-views/#createmodelmixin
I found K. Moe's answer to this question: Django Rest Framework POST Update if existing or create much easier to understand and implement. You only need to add a create method in the serializer and use mixins.CreateModelMixin, generics.GenericAPIView in the view. Then you can use a POST request, instead of PUT/PATCH. It allows to create AND update data stored in your database. My code for the view:
class ZipCodeList(mixins.CreateModelMixin, generics.GenericAPIView):
def post(self, request, format=None):
is_many = isinstance(request.data, list)
if is_many:
serializer = ZipCodeSerializer(data=request.data, many=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
serializer = ZipCodeSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#Greg Holst, why so much duplication? Why not:
class ZipCodeList(mixins.CreateModelMixin, generics.GenericAPIView):
def post(self, request, format=None):
serializer = ZipCodeSerializer(data=request.data, many=isinstance(request.data, list))
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Anyway, that only worked for me for creating new objects, didn't work to create-or-update in one sweep (it kept telling me these objects already exist), so this is what I did:
class ContributorSyncListAPIView(ListAPIView):
permission_classes = (isSuperUserPermission,)
allowed_methods = ("GET", "PUT")
lookup_field = "airtable_id"
serializer_class = ContributorSyncSerializer # Doesn't get used in Put, just in Get.
model = Contributor
def get_queryset(self):
return self.model.objects.all()
def put(self, request, format=None):
objects = list(request.data)
# objects is a list of OrderedDicts
try:
for o in objects:
try:
instance = self.model.objects.get(
**{self.lookup_field: o.get(self.lookup_field)}
)
for key, value in o.items():
setattr(instance, key, value)
except self.model.DoesNotExist:
instance = self.model(**o)
instance.save()
return Response(objects, status=status.HTTP_200_OK)
except Exception as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
Note my code above is very light on validation just because it was for a process of syncing models from one environment to another by a superuser, different environments with an identical codebase; so the assumption was the data had already been validated when the data was entered into the first environment. For any other purpose you'd want to validate more. But this is what I had to do to handle a list of objects that may need to be created or updating, on an object-by-object basis.

Django RestFramework Serializer request.data with array object

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)

Add extract fields to Django model serializer while doing validations

Supposed I had the following model, and modelSerializer:
models.py
class Approve(models.Model):
process = models.IntegerField(verbose_name='Associated Process')
content = models.CharField(max_length=300, verbose_name="Approval Content")
serializers.py
class ApproveSerializer(serializers.ModelSerializer):
class Meta:
model = Approve
fields = ('id', 'process' ,'content')
def validate(self, value):
process = value['process']
try:
something_else = Something.objects.get(process)
except Something.DoesNotExist:
raise serializers.ValidationError('Invalid Process')
return value
The thing is that I want to validate the incoming data in serializers.py, instead of views.py. You can notice that in order to make a validation check, I had query the database for something_else.
The problem is that I want to use this object something_else in the views.py, instead of making another database query. Is there any ways I can pass it with the serializer, without causing a serializer validation error when call serializer.is_valid() method.
Any suggestions will be welcomed, thanks in advance.
Well i am not sure how your views.py might look but essentially, you will need to instantiate the serializer there:
def approve_view(request):
if request.method == "POST":
process = request.POST.get('value', None)
something_else = Something.objects.filter(process=process).first()
serializer = ApproveSerializer(something_else=something_else) # This is where you pass the something_else object.
if serializer.is_valid():
# Your code for successful validation.
else:
# Your code for failed validation
Now that you have passed something_else into the ApproveSerializer, you need to set it up as a property:
class ApproveSerializer(serializers.ModelSerializer):
def __init__(self, something_else=None):
self.something_else = something_else
class Meta:
model = Approve
fields = ('id', 'process' ,'content')
def validate(self, value):
process = value['process']
if not self.something_else:
raise serializers.ValidationError('Invalid Process')
return value
I figure out a way to address the problem.
serializers.py
class ApproveSerializer(serializers.ModelSerializer):
class Meta:
model = Approve
fields = ('id', 'process' ,'content')
def validate(self, value):
process = value['process']
try:
something_else = Something.objects.get(process)
value['something_else'] = something_else
except Something.DoesNotExist:
raise serializers.ValidationError('Invalid Process')
return value
views.py
Before calling the serializer.save() method, you should pop the value that had been added to the serializer.
def post(self, request):
serializer = ApproveSerializer(data=request.data)
if serializer.is_valid():
something_else = serializer.validated_data.pop('something_else')
something_else.property = new_property
something_else.save()
serializer.save()
else:
# Error handling goes here serialzier.errors
Not sure this is a good practice, but at least it works for me right now. Hoping to know a better solution.

Categories