Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to update multiple fields of Django model object

Tags:

python

django

I'm trying to update user in Django database.

Fetched data is as follows :

fetched_data = {      'id': 1,      'first_name': 'John',      'last_name': 'Doe',      'phone': '+32 12',      'mobile_phone': '+32 13',      'email': '[email protected]',      'username': 'myusername' } 

I get the user with this id as follows :

old_user = User.objects.get(pk=fetched_data['id']) 

If I update the user as follows :

old_user.username = fetched_data['username'] old_user.first_name = fetched_data['first_name'] ...... old_user.save() 

it works fine, but I do not want to do it for every record, thus I tried something like :

for fetched_data_key in fetched_data:     old_user.fetched_data_key = fetched_data['fetched_data_key']     //old_user[fetched_data_key] = fetched_data['fetched_data_key'] --- I tried this way to     old_user.save() 

But that doesn't work. Any idea how can I update the user without doing it for every record?

like image 645
Boky Avatar asked Jan 19 '17 14:01

Boky


People also ask

How do I update model fields in Django?

Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.

How do I update Django bulk records?

The best solutions I found are: a) use @transaction. atomic decorator, which improves performance by using a single transaction, or b) make a bulk insert in a temporary table, and then an UPDATE from the temporary table to the original one.

How do you add a new field to a model with new Django migrations?

To answer your question, with the new migration introduced in Django 1.7, in order to add a new field to a model you can simply add that field to your model and initialize migrations with ./manage.py makemigrations and then run ./manage.py migrate and the new field will be added to your DB.

How do you make two fields unique in Django?

Using the constraints features UniqueConstraint is preferred over unique_together. From the Django documentation for unique_together : Use UniqueConstraint with the constraints option instead. UniqueConstraint provides more functionality than unique_together.


1 Answers

You can update a row in the database without fetching and deserializing it; update() can do it. E.g.:

User.objects.filter(id=data['id']).update(email=data['email'], phone=data['phone']) 

This will issue one SQL update statement, and is much faster than the code in your post. It will never fetch the data or waste time creating a User object.

You cannot, though, send a whole bunch of update data to the SQL database and ask it to map it to different rows in one go. If you need a massive update like that done very quickly, your best bet is probably inserting the data into a separate table and then update it form a select on that table. Django ORM does not support this, as far as I can tell.

like image 50
9000 Avatar answered Sep 21 '22 23:09

9000