mirror of https://github.com/django/django.git
Fixed #31382 -- Made Model.save(update_fields=...) raise ValueError on non-concrete fields.
This commit is contained in:
parent
94ea79be13
commit
8954f255bb
|
@ -727,7 +727,7 @@ class Model(metaclass=ModelBase):
|
||||||
update_fields = frozenset(update_fields)
|
update_fields = frozenset(update_fields)
|
||||||
field_names = set()
|
field_names = set()
|
||||||
|
|
||||||
for field in self._meta.fields:
|
for field in self._meta.concrete_fields:
|
||||||
if not field.primary_key:
|
if not field.primary_key:
|
||||||
field_names.add(field.name)
|
field_names.add(field.name)
|
||||||
|
|
||||||
|
@ -737,9 +737,11 @@ class Model(metaclass=ModelBase):
|
||||||
non_model_fields = update_fields.difference(field_names)
|
non_model_fields = update_fields.difference(field_names)
|
||||||
|
|
||||||
if non_model_fields:
|
if non_model_fields:
|
||||||
raise ValueError("The following fields do not exist in this "
|
raise ValueError(
|
||||||
"model or are m2m fields: %s"
|
'The following fields do not exist in this model, are m2m '
|
||||||
% ', '.join(non_model_fields))
|
'fields, or are non-concrete fields: %s'
|
||||||
|
% ', '.join(non_model_fields)
|
||||||
|
)
|
||||||
|
|
||||||
# If saving to the same database, and this model is deferred, then
|
# If saving to the same database, and this model is deferred, then
|
||||||
# automatically do an "update_fields" save on the loaded fields.
|
# automatically do an "update_fields" save on the loaded fields.
|
||||||
|
|
|
@ -22,9 +22,19 @@ class Employee(Person):
|
||||||
accounts = models.ManyToManyField('Account', related_name='employees', blank=True)
|
accounts = models.ManyToManyField('Account', related_name='employees', blank=True)
|
||||||
|
|
||||||
|
|
||||||
|
class NonConcreteField(models.IntegerField):
|
||||||
|
def db_type(self, connection):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_attname_column(self):
|
||||||
|
attname, _ = super().get_attname_column()
|
||||||
|
return attname, None
|
||||||
|
|
||||||
|
|
||||||
class Profile(models.Model):
|
class Profile(models.Model):
|
||||||
name = models.CharField(max_length=200)
|
name = models.CharField(max_length=200)
|
||||||
salary = models.FloatField(default=1000.0)
|
salary = models.FloatField(default=1000.0)
|
||||||
|
non_concrete = NonConcreteField()
|
||||||
|
|
||||||
|
|
||||||
class ProxyEmployee(Employee):
|
class ProxyEmployee(Employee):
|
||||||
|
|
|
@ -5,7 +5,10 @@ from .models import Account, Employee, Person, Profile, ProxyEmployee
|
||||||
|
|
||||||
|
|
||||||
class UpdateOnlyFieldsTests(TestCase):
|
class UpdateOnlyFieldsTests(TestCase):
|
||||||
msg = 'The following fields do not exist in this model or are m2m fields: %s'
|
msg = (
|
||||||
|
'The following fields do not exist in this model, are m2m fields, or '
|
||||||
|
'are non-concrete fields: %s'
|
||||||
|
)
|
||||||
|
|
||||||
def test_update_fields_basic(self):
|
def test_update_fields_basic(self):
|
||||||
s = Person.objects.create(name='Sara', gender='F')
|
s = Person.objects.create(name='Sara', gender='F')
|
||||||
|
@ -254,3 +257,8 @@ class UpdateOnlyFieldsTests(TestCase):
|
||||||
self.assertEqual(Person.objects.count(), 1)
|
self.assertEqual(Person.objects.count(), 1)
|
||||||
with self.assertNumQueries(2):
|
with self.assertNumQueries(2):
|
||||||
s.save(update_fields=['name', 'employee_num'])
|
s.save(update_fields=['name', 'employee_num'])
|
||||||
|
|
||||||
|
def test_update_non_concrete_field(self):
|
||||||
|
profile_boss = Profile.objects.create(name='Boss', salary=3000)
|
||||||
|
with self.assertRaisesMessage(ValueError, self.msg % 'non_concrete'):
|
||||||
|
profile_boss.save(update_fields=['non_concrete'])
|
||||||
|
|
Loading…
Reference in New Issue