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.
Related
I have a model and I want to write an update() method for it in order to update.
The below snippet is my model:
class Klass(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=500)
university = models.CharField(max_length=50,blank=True, null=True)
teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE)
and the below snippet is corresponding Serializer:
class KlassSerializer(ModelSerializer):
teacher = ProfileSerializer()
url = HyperlinkedIdentityField(view_name='mainp-api:detail', lookup_field='pk')
klass_settings = KlassSettingsSerializer()
class Meta:
model = Klass
fields = ('url', 'id', 'title', 'description', 'university','teacher')
def update(self, instance, validated_data):
instance.title = validated_data.get('title', instance.title)
instance.description = validated_data.get('description', instance.description)
instance.university = validated_data.get('university', instance.university)
instance.save()
return instance
And for update, I use below snippet:
class KlassAPIView(APIView):
def put(self, request, pk=None):
if pk == None:
return Response({'message': 'You must specify class ID'}, status=HTTP_400_BAD_REQUEST)
klass = Klass.objects.get(pk=pk)
if request.user.profile.type != 't':
raise PermissionDenied(detail={'message': 'You aren't teacher of this class, so you can't edit information.'})
serializer = KlassSerializer(data=request.data, context={'request': request})
serializer.initial_data['teacher'] = request.user.profile.__dict__
if serializer.is_valid():
serializer.update(instance=klass, validated_data=serializer.data) # Retrieve teacher and store
return Response({'data': serializer.data}, status=HTTP_200_OK)
else:
return Response({'data': serializer.errors}, status=HTTP_400_BAD_REQUEST)
but when I send data with PUT method, it returns below error:
AttributeError at /api/class/49/
'collections.OrderedDict' object has no attribute 'pk'
and the error occurs in serializer.update(instance=klass, validated_data=serializer.data) line.
Just ran into the same error.
In my case the problem was I accessed serializer.data before doing serializer.save().
Google dropped me here, so maybe someone else will also find this helpful.
Source: https://github.com/encode/django-rest-framework/issues/2964
i don't know if this helps. I always add the id field in the serializer due to that similar issue:
id = serializers.ModelField(model_field=YourModel._meta.get_field('id'), required=False)
Make sure it's required=False because when you create a new record the id field is not present.
Well in my case, I was doing:
champions_list = []
for champion in champions_serializer.data:
c = {"id": champion.id}
champions_list.append(c)
And the correct way to do it is:
champions_list = []
for champion in champions_serializer.data:
c = {"id": champion["id"]}
champions_list.append(c)
And make sure that you return the id inside the serializer.
Many answers to this question note that serializer.save() must be called before using serializer.data.
In my case, I was definitely calling serializer.save(), however, I was overriding the save method on my serializer and did not set self.instance on the serializer in that method.
So if you are overriding save be sure to do:
class MySerializer(serializers.ModelSerializer):
def save(self, *args, **kwargs):
...
self.instance = instance
return self.instance
I have a custom validation function in my DataSerliazer which checks two parameters (mrange , mthreshold). The validation function checks that only one of the parameters needs to be set while posting.
class DataSerializer(serializers.ModelSerializer):
emails = serializers.ListField(child = serializers.EmailField())
class Meta:
model = AIData
fields = ('id', 'name', 'created', 'username', 'token',
'expression','key','threshold' ,'evaluator', 'range','emails','metric_name', 'status')
def validate(self,attrs):
mrange = attrs.get("metric_range")
mthreshold = attrs.get("metric_threshold")
if (mrange == None or mrange == " ") and (mthreshold == None or mthreshold == " "):
raise serializers.ValidationError({'error': 'Cannot have both range and threshold empty'})
elif mrange != None and mthreshold != None:
raise serializers.ValidationError({'error': 'Cannot set both range and threshold'})
In my views.py file
#api_view(['GET','PUT', 'DELETE'])
def ai_detail(request, token, id):
ai = None
try:
ai = AIData.objects.get(id=id)
except AIData.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = DataSerializer(ai)
if serializer.data['token'] == token:
return Response(serializer.data)
else:
return Response({'error': 'Not allowed to access'}, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'PUT':
serializer = AlAnotherSerializerMethod(alert, data=request.data, partial=True)
if serializer.is_valid():
// updating code
Currently, I have to write anthor AIAnotherSerializerMethod so that, it does not implicitly call the validate function in DataSerializer. This is to avoid checking the 2 parameters during object creation. Once the object is created , I dont want to check the same function anymore. Is there a neater way of doing the same ? I would preferably avoid the need for writing 2 Serializers for the same model.
You can pass context data to the serializer which could include the request itself.
You can access the context anytime anywhere in the serializer by calling self.context.
This solution does makes it easier to provide contextual data without overriding the __init__.
The rest-framework library doesn't have anything like that as far as I know.
The way I do it is to overide the __init__ and add your own variable to check this.
class DataSerializer(serializers.ModelSerializer):
emails = serializers.ListField(child = serializers.EmailField())
class Meta:
model = AIData
fields = ('id', 'name', 'created', 'username', 'token',
'expression','key','threshold' ,'evaluator', 'range','emails','metric_name', 'status')
def __init__(self, *args, **kwargs):
super(DataSerializer, self).__init__(*args, **kwargs)
self._request_method = kwargs.get('request_method', 'GET')
def validate(self,attrs):
if self._request_method == 'GET':
# GET logic
elif self._request_method in ('PUT', 'POST'):
# Put or Post logic
And then i'd use it like this:
serializer = DataSerializer(ai, request_method=request.method)
Good morning everybody!
I've a problem with django REST ModelSerializer.
This is my serializer:
class TransactionPayOutSerializer(serializers.ModelSerializer):
author = serializers.SlugRelatedField(queryset=Users.objects.all(), slug_field='unique_id', allow_null=True)
wallet = serializers.SlugRelatedField(queryset=Wallet.objects.all(), slug_field='wallet_id')
bank_account = serializers.SlugRelatedField(queryset=BankAccount.objects.all(), slug_field='bank_account_id')
class Meta:
model = PayOut
fields = ('author', 'wallet', 'amount',
'currency', 'bank_account')
def __init__(self, **kwargs):
if 'instance' in kwargs:
super(TransactionPayOutSerializer,self).__init__(kwargs.get('instance'))
else:
self.merchant = kwargs.pop('merchant')
super(TransactionPayOutSerializer, self).__init__(**kwargs)
def validate(self, data):
print 'Inside serializer validate(), data:'
print data
wallet = data['wallet']
user_wallet = UserWallets.objects.get(wallet=wallet)
if self.merchant != user_wallet.merchant:
raise serializers.ValidationError("Wallet not found")
wallet.fetch()
if wallet.balance < data['amount']:
raise serializers.ValidationError("Not enough founds in wallet")
return data
def create(self, validated_data):
print 'Inside TransactionPayOutSerializer create():'
print 'Validated data:'
print validated_data
pay_out = PayOut(**validated_data)
user_wallet = UserWallets.objects.get(wallet=validated_data['wallet'])
pay_out.author = user_wallet.user
pay_out.merchant = self.merchant
pay_out.save()
return pay_out
It works perfectly but my problem is:
When the serializer is used for a get request (serializer.data) it's perfect.
But when the serializer is used for a post request (TransactionPayOutSerializer(data=request.DATA)) it absolutely wants the 'user' parameter in the JSON.
What is the best way to keep serialzer.data return 'user' representation making TransactionPayOutSerializer(data=request.DATA) works without receiving 'user' as parameter inside JSON post request?
I should use simple serializers instead of ModelSerializers?
Thanks in advance!
Serializer Fields
read_only Set this to True to ensure that the field is used when
serializing a representation, but is not used when creating or
updating an instance during deserialization.
Defaults to False
write_only Set this to True to ensure that the field may be used when
updating or creating an instance, but is not included when serializing
the representation.
Defaults to False
I want to pass some arguments to DRF Serializer class from Viewset, so for I have tried this:
class OneZeroSerializer(rest_serializer.ModelSerializer):
def __init__(self, *args, **kwargs):
print args # show values that passed
location = rest_serializer.SerializerMethodField('get_alternate_name')
def get_alternate_name(self, obj):
return ''
class Meta:
model = OneZero
fields = ('id', 'location')
Views
class OneZeroViewSet(viewsets.ModelViewSet):
serializer_class = OneZeroSerializer(realpart=1)
#serializer_class = OneZeroSerializer
queryset = OneZero.objects.all()
Basically I want to pass some value based on querystring from views to Serializer class and then these will be allocate to fields.
These fields are not include in Model in fact dynamically created fields.
Same case in this question stackoverflow, but I cannot understand the answer.
Can anyone help me in this case or suggest me better options.
It's very easy with "context" arg for "ModelSerializer" constructor.
For example:
in view:
my_objects = MyModelSerializer(
input_collection,
many=True,
context={'user_id': request.user.id}
).data
in serializers:
class MyModelSerializer(serializers.ModelSerializer):
...
is_my_object = serializers.SerializerMethodField('_is_my_find')
...
def _is_my_find(self, obj):
user_id = self.context.get("user_id")
if user_id:
return user_id in obj.my_objects.values_list("user_id", flat=True)
return False
...
so you can use "self.context" for getting extra params.
Reference
You could in the YourView override get_serializer_context method like that:
class YourView(GenericAPIView):
def get_serializer_context(self):
context = super().get_serializer_context()
context["customer_id"] = self.kwargs['customer_id']
context["query_params"] = self.request.query_params
return context
or like that:
class YourView(GenericAPIView):
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.context["customer_id"] = request.user.id
serializer.context["query_params"] = request.query_params
serializer.is_valid(raise_exception=True)
...
and anywhere in your serializer you can get it. For example in a custom method:
class YourSerializer(ModelSerializer):
def get_alternate_name(self, obj):
customer_id = self.context["customer_id"]
query_params = self.context["query_params"]
...
To fulfill the answer of redcyb - consider using in your view the get_serializer_context method from GenericAPIView, like this:
def get_serializer_context(self):
return {'user': self.request.user.email}
A old code I wrote, that might be helpful- done to filter nested serializer:
class MySerializer(serializers.ModelSerializer):
field3 = serializers.SerializerMethodField('get_filtered_data')
def get_filtered_data(self, obj):
param_value = self.context['request'].QUERY_PARAMS.get('Param_name', None)
if param_value is not None:
try:
data = Other_model.objects.get(pk_field=obj, filter_field=param_value)
except:
return None
serializer = OtherSerializer(data)
return serializer.data
else:
print "Error stuff"
class Meta:
model = Model_name
fields = ('filed1', 'field2', 'field3')
How to override get_serializer_class:
class ViewName(generics.ListAPIView):
def get_serializer_class(self):
param_value = self.context['request'].QUERY_PARAMS.get('Param_name', None)
if param_value is not None:
return Serializer1
else:
return Serializer2
def get_queryset(self):
.....
Hope this helps people looking for this.
List of element if your query is a list of elements:
my_data = DataSerializers(queryset_to_investigate,
many=True, context={'value_to_pass': value_passed}
in case off single data query:
my_data = DataSerializers(queryset_to_investigate,
context={'value_to_pass': value_passed}
Then in the serializers:
class MySerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = 'Name_of_your_model'
def on_representation(self, value):
serialized_data = super(MySerializer, self).to_representation(value)
value_as_passed = self.context['value_to_pass']
# ..... do all you need ......
return serialized_data
As you can see printing the self inside on_representation you can see: query_set: <object (x)>, context={'value_to_pass': value_passed}
This is a simpler way, and you can do this in any function of serializers having self in the parameter list.
These answers are far to complicated; If you have any sort of authentication then add this property to your serializer and call it to access the user sending the request.
class BaseSerializer(serializers.ModelSerializer):
#property
def sent_from_user(self):
return self.context['request'].user
Getting the context kwargs passed to a serializer like;
...
self.fields['category'] = HouseCategorySerializer(read_only=True, context={"all_fields": False})
...
In your serializer, that is HouseCategorySerializer do this in one of your functions
def get_houses(self, instance):
print(self._context.get('all_fields'))
Using self._context.get('keyword') solved my mess quickly, instead of using self.get_extra_context()
I use modelformset_factory, and I use full_clean() to validate the form with unique_together=True. I wonder what is the best way to handle error in case the unique_together do not validate in order to return the error message in the template.
Please take a look to my view, and tell me if im correct the way I do it, or if there is a better approach.
model:
class Attribute(models.Model):
shapefile = models.ForeignKey(Shapefile)
name = models.CharField(max_length=255, db_index=True)
type = models.IntegerField()
width = models.IntegerField()
precision = models.IntegerField()
def __unicode__(self):
return self.name
def delete(self):
shapefile = self.shapefile
feature_selected = Feature.objectshstore.filter(shapefile=shapefile)
feature_selected.hremove('attribute_value', self.name)
super(Attribute, self).delete()
class Meta:
unique_together = (('name', 'shapefile'),)
form:
class AttributeForm(ModelForm):
def __init__(self, *args, **kwargs):
super(AttributeForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.pk:
self.fields['type'].widget.attrs['disabled'] = True
self.fields['type'].required = False
self.fields['width'].widget.attrs['readonly'] = True
self.fields['precision'].widget.attrs['readonly'] = True
def clean_type(self):
if self.instance and self.instance.pk:
return self.instance.type
else:
return self.cleaned_data['type']
type = forms.ChoiceField(choices=FIELD_TYPE)
class Meta:
model = Attribute
exclude = 'shapefile'
view:
def editFields(request, shapefile_id):
layer_selected = Shapefile.objects.get(pk=shapefile_id)
attributes_selected= Attribute.objects.filter(shapefile__pk=shapefile_id)
attributesFormset = modelformset_factory(Attribute, form=AttributeForm, extra=1, can_delete=True)
if request.POST:
formset = attributesFormset(request.POST, queryset=attributes_selected)
if formset.is_valid():
instances = formset.save(commit=False)
for instance in instances:
instance.shapefile = layer_selected
try:
instance.full_clean()
except ValidationError as e:
non_field_errors = e.message_dict[NON_FIELD_ERRORS]
print non_field_errors
formset = attributesFormset(queryset=attributes_selected)
return render_to_response("basqui/manage_layer_editFields.html", {'shapefile': layer_selected, 'formset':formset}, context_instance=RequestContext(request))
instance.save()
formset = attributesFormset(queryset=attributes_selected)
return render_to_response("basqui/manage_layer_editFields.html", {'shapefile': layer_selected, 'formset':formset}, context_instance=RequestContext(request))
The disadvantage of your approach is that you have moved the validation from the form to the view.
I had the same problem recently of validating a unique together constraint where one field is excluded from the model form. My solution was to override the model form's clean method, and query the database to check the unique together constraint. This duplicates the code that is called by full_clean, but I like it because it's explicit.
I briefly thought about overriding _get_validation_exclusions which would have been more DRY, but I decided not to rely on a private api.