Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #18362 - Made model.save() update_fields accept attnames

  • Loading branch information...
commit 0f49b2bce2d5b3c5891c8a329bab7dffe16fc79b 1 parent e4a1407
Andrey Antukh niwibe authored akaariai committed
13 django/db/models/base.py
View
@@ -468,8 +468,15 @@ def save(self, force_insert=False, force_update=False, using=None,
return
update_fields = frozenset(update_fields)
- field_names = set([field.name for field in self._meta.fields
- if not field.primary_key])
+ field_names = set()
+
+ for field in self._meta.fields:
+ if not field.primary_key:
+ field_names.add(field.name)
+
+ if field.name != field.attname:
+ field_names.add(field.attname)
+
non_model_fields = update_fields.difference(field_names)
if non_model_fields:
@@ -534,7 +541,7 @@ def save_base(self, raw=False, cls=None, origin=None, force_insert=False,
non_pks = [f for f in meta.local_fields if not f.primary_key]
if update_fields:
- non_pks = [f for f in non_pks if f.name in update_fields]
+ non_pks = [f for f in non_pks if f.name in update_fields or f.attname in update_fields]
# First, try an UPDATE. If that doesn't update anything, do an INSERT.
pk_val = self._get_pk_val(meta)
8 tests/modeltests/update_only_fields/tests.py
View
@@ -55,6 +55,14 @@ def test_update_fields_inheritance(self):
self.assertEqual(e3.name, 'Ian')
self.assertEqual(e3.profile, profile_receptionist)
+ with self.assertNumQueries(1):
+ e3.profile = profile_boss
+ e3.save(update_fields=['profile_id'])
+
+ e4 = Employee.objects.get(pk=e3.pk)
+ self.assertEqual(e4.profile, profile_boss)
+ self.assertEqual(e4.profile_id, profile_boss.pk)
+
def test_update_fields_inheritance_with_proxy_model(self):
profile_boss = Profile.objects.create(name='Boss', salary=3000)
profile_receptionist = Profile.objects.create(name='Receptionist', salary=1000)
Please sign in to comment.
Something went wrong with that request. Please try again.