Update only selected model fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from django.db import models, connection, transaction

def update_fields(self, **kwargs):
    """                                                                         
    Update selected model fields in the database, but leave the other           
    fields alone. Use this rather than model.save() for performance             
    and data consistency.                                                       
    """
    sql = ['UPDATE', connection.ops.quote_name(self._meta.db_table), 'SET']
    values = []
    for field_name in kwargs:
        setattr(self, field_name, kwargs[field_name])
        field = self._meta.get_field(field_name)
        value = field.get_db_prep_save(kwargs[field_name])
        if isinstance(value, models.Model):
            value = value.id
        sql.extend((connection.ops.quote_name(field.column), '=', '%s', ','))
        values.append(value)
    sql.pop(-1) # Remove the last comma                                         
    sql.extend(['WHERE', 'id', '=', '%s'])
    values.append(self.id)
    sql = ' '.join(sql)
    connection.cursor().execute(sql, values)
    transaction.commit_unless_managed()

update_fields.alters_data = True

More like this

  1. PatchModelForm - A ModelForm subclass with the semantics of the PATCH HTTP method by gnrfan 1 year, 11 months ago
  2. Improved Pickled Object Field by taavi223 4 years, 8 months ago
  3. Display values from a bound (submitted) form by masida 1 month, 1 week ago
  4. Mobilize your Django site by stevena0 5 years ago
  5. DKIM Email Backend by DrMeers 4 years ago

Comments

dotsbb (on May 25, 2010):

Too lengthy.

The same functionality in one line:

def update_fields(self, **kwargs):
    self.objects.filter(pk=self.pk).update(**kwargs)
update_fields.alters_data = True

#

dotsbb (on May 31, 2010):

def update_fields(self, **kwargs):
    self.__class__.objects.filter(pk=self.pk).update(**kwargs)

#

(Forgotten your password?)