=== modified file 'django/db/models/base.py'
|
|
|
|
| 498 | 498 | # It does already exist, so do an UPDATE. |
| 499 | 499 | if force_update or non_pks: |
| 500 | 500 | values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks] |
| 501 | | rows = manager.using(using).filter(pk=pk_val)._update(values) |
| 502 | | if force_update and not rows: |
| 503 | | raise DatabaseError("Forced update did not affect any rows.") |
| | 501 | if values: |
| | 502 | rows = manager.using(using).filter(pk=pk_val)._update(values) |
| | 503 | if force_update and not rows: |
| | 504 | raise DatabaseError("Forced update did not affect any rows.") |
| 504 | 505 | else: |
| 505 | 506 | record_exists = False |
| 506 | 507 | if not pk_set or not record_exists: |
=== modified file 'tests/modeltests/force_insert_update/models.py'
|
|
|
|
| 3 | 3 | automatic behaviour). |
| 4 | 4 | """ |
| 5 | 5 | from django.db import models, transaction, IntegrityError |
| | 6 | from django.test import TestCase |
| 6 | 7 | |
| 7 | 8 | class Counter(models.Model): |
| 8 | | name = models.CharField(max_length = 10) |
| | 9 | name = models.CharField(max_length=10) |
| 9 | 10 | value = models.IntegerField() |
| 10 | 11 | |
| | 12 | class InheritedCounter(Counter): |
| | 13 | tag = models.CharField(max_length=10) |
| | 14 | |
| | 15 | class ProxyCounter(Counter): |
| | 16 | class Meta: |
| | 17 | proxy = True |
| | 18 | |
| | 19 | class SubCounter(Counter): |
| | 20 | pass |
| | 21 | |
| 11 | 22 | class WithCustomPK(models.Model): |
| 12 | 23 | name = models.IntegerField(primary_key=True) |
| 13 | 24 | value = models.IntegerField() |
| 14 | 25 | |
| | 26 | class InheritanceTests(TestCase): |
| | 27 | def test_force_update_on_inherited_model(self): |
| | 28 | a = InheritedCounter(name="count", value=1, tag="spam") |
| | 29 | a.save() |
| | 30 | a.save(force_update=True) |
| | 31 | |
| | 32 | def test_force_update_on_proxy_model(self): |
| | 33 | a = ProxyCounter(name="count", value=1) |
| | 34 | a.save() |
| | 35 | a.save(force_update=True) |
| | 36 | |
| | 37 | def test_force_update_on_inherited_model_without_fields(self): |
| | 38 | ''' |
| | 39 | Issue 13864: force_update fails on subclassed models, if they don't |
| | 40 | specify custom fields. |
| | 41 | ''' |
| | 42 | a = SubCounter(name="count", value=1) |
| | 43 | a.save() |
| | 44 | a.value = 2 |
| | 45 | a.save(force_update=True) |
| | 46 | |
| 15 | 47 | __test__ = {"API_TESTS": """ |
| 16 | 48 | >>> c = Counter.objects.create(name="one", value=1) |
| 17 | 49 | |