last day (31 days later) » 

9:45 AM
0
A: instance.save() is not saving the model in ListSerializer Django Rest Framework

MehranTo call the .save() method, you have to call it on an instance of a Model, not on a QuerySet of the model. According to DRF Docs, class BookListSerializer(serializers.ListSerializer): def update(self, instance, validated_data): # Maps for id->instance and id->data item. book_mapping = ...

 
In my code I'm doing like this NoAccessDetailsSerializer(filtered_id_data, data=request.data, many=True ) where filtered_id_data is the queryset. Instead of passing the queryset what I need to pass?
I'll change code and let you know if this works
 
Sure! Let me know if it worked! I personally have never really bothered using ListSerializer and always stuck with ModelSerializer or basic Serializer (if not connected with any Model)
 
And wanted to know if you had a list of objects which needs to be updated how would you do? In the documentation itself, they have mentioned to go with the ListSerializer.
hey @Mehran the above code actually worked. But still, instance[index].save() is not saving the data in the database. Whereas in the update() in ModelSerializer saves the data by calling instance.save(). Do you have any idea?
 
For a list, I would loop through the validated_data list in my view and call the modelserializer for each item. I checked the ListSerializer implementation in the DRF serializers.py file. For the update method: Serializers with many=True do not support multiple update by default, only multiple create. For updates it is unclear how to deal with insertions and deletions. If you need to support multiple update, use a ListSerializer class and override .update() so you can specify the behavior exactly. So, no I am kinda confused since the writers themselves are not clear about this.
@AashayAmballi you are using enumerate on validated data. This will create problems in mapping your object instance (not the list) with the appropriate validated data. Please refer to the docs and fix this.
@AashayAmballi I updated my answer according to the docs, see if it helps!
 
Yeah I will try and let you know. :)
Hey @Mehran I tried what you suggested. Since in my model the user_working field has a foreign key relationship with a custom user model, the user_working field is not getting saved in the database. But the row_chosen data is getting saved :(
 
9:45 AM
in the user_chosen you are passing the name. Just pass the 'id' of the user. Something like this: obj.user_working_id = data['user_working_id'] The data being passed: [{"id": 1, "row_chosen": false, "user_working_id": 33},]
 
I'm actually passing the id of the user. Just for demonstration purpose, I had mentioned the user name.
I actually did obj.user_id = data["user_working"].user_id and also this obj["user_working"] = data["user_working"].user_id. It even prints out the user id as well. But when it saves, it saves None. I have no idea what's going wrong! row_chosen data is getting saved but user_working is not.
 
Here data is a json. It should not contain the 'user_working' object, but rather a to_string representation sent by the client side. You have to pass the id from the client side.
 
Yes @Mehran , data is a json object when it comes to ListSerializer the user_working key will have user model instance as value
 
Hmm. could you provide your model definition please?
 
Hey @Mehran! I don't know really, all of a sudden it started working fine. Now the user_working and row_chosen data are getting saved in the database. Thank you, my friend. Really much appreciated for helping me throughout this issue. Thank you :)
which model definition? User or NoAccessDetail?
 
9:45 AM
NoAccessDetail model. Also please check if the object you're passing in the update method to the field user_working is a User object.
 
Yes it's an user instance. And NoAccessDetail model is kinda huge. It has many fields. Where can I share you the model?
 
update your question with the model definition.
 
I've updated the question with the model definition for both NoAccessDetails and UserProfile.
 
@Aashay Amballi Oh it's already working! I was really since it shouldn't Not work xD. Nice solving a weird problem together! And please up vote my answer if possible :)
 
Yes my friend... Really much appreciated for solving this issue with me. Thank you 😊😊😊
 

last day (31 days later) »