| 1 | """ |
| 2 | #. Save only modified fields on update |
| 3 | """ |
| 4 | from django.db import models |
| 5 | |
| 6 | class Article(models.Model): |
| 7 | headline = models.CharField(maxlength=100, default='Default headline') |
| 8 | body = models.TextField() |
| 9 | pub_date = models.DateTimeField() |
| 10 | |
| 11 | class Meta: |
| 12 | ordering = ('pub_date','headline') |
| 13 | |
| 14 | def __str__(self): |
| 15 | return self.headline |
| 16 | |
| 17 | __test__ = {'API_TESTS':""" |
| 18 | # Create a couple of Articles. |
| 19 | >>> from datetime import datetime |
| 20 | >>> a = Article(headline='Article 1', body='Body 1', pub_date=datetime(2007, 5, 5)) |
| 21 | >>> a.save() |
| 22 | |
| 23 | # Change headline and body on different instances, then save both to verify |
| 24 | # that each only updated the modified field. |
| 25 | >>> a1 = Article.objects.get(pk=a.id) |
| 26 | >>> a2 = Article.objects.get(pk=a.id) |
| 27 | >>> a1.headline = 'Changed article 1' |
| 28 | >>> a2.body = 'Changed body 1' |
| 29 | >>> a1.save() |
| 30 | >>> a2.save() |
| 31 | >>> a3 = Article.objects.get(pk=a.id) |
| 32 | >>> a3.headline |
| 33 | u'Changed article 1' |
| 34 | >>> a3.body |
| 35 | u'Changed body 1' |
| 36 | |
| 37 | # Fields entered at instantiation of a model which will already exists should be |
| 38 | # saved as well. |
| 39 | >>> a = Article(id=a.id, headline='Reset article 1', body='Reset body 1', pub_date=datetime(2007, 5, 5)) |
| 40 | >>> a.save() |
| 41 | >>> a3 = Article.objects.get(pk=a.id) |
| 42 | >>> a3.headline |
| 43 | u'Reset article 1' |
| 44 | >>> a3.body |
| 45 | u'Reset body 1' |
| 46 | |
| 47 | # If the value doesn't change, it won't be marked as modified -- otherwise user |
| 48 | # input like forms/etc is going to mark everything modified regardless. |
| 49 | >>> a1 = Article.objects.get(pk=a.id) |
| 50 | >>> a2 = Article.objects.get(pk=a.id) |
| 51 | >>> a1.headline = 'Changed article 2' |
| 52 | >>> a2.headline = a2.headline |
| 53 | >>> a2.body = 'Changed body 2' |
| 54 | >>> a1.save() |
| 55 | >>> a2.save() |
| 56 | >>> a3 = Article.objects.get(pk=a.id) |
| 57 | >>> a3.headline |
| 58 | u'Changed article 2' |
| 59 | >>> a3.body |
| 60 | u'Changed body 2' |
| 61 | """} |
| 62 | """ |
| 63 | #. Save only modified fields on update |
| 64 | """ |
| 65 | from django.db import models |
| 66 | |
| 67 | class Article(models.Model): |
| 68 | headline = models.CharField(maxlength=100, default='Default headline') |
| 69 | body = models.TextField() |
| 70 | pub_date = models.DateTimeField() |
| 71 | |
| 72 | class Meta: |
| 73 | ordering = ('pub_date','headline') |
| 74 | |
| 75 | def __str__(self): |
| 76 | return self.headline |
| 77 | |
| 78 | __test__ = {'API_TESTS':""" |
| 79 | # Create a couple of Articles. |
| 80 | >>> from datetime import datetime |
| 81 | >>> a = Article(headline='Article 1', body='Body 1', pub_date=datetime(2007, 5, 5)) |
| 82 | >>> a.save() |
| 83 | |
| 84 | # Change headline and body on different instances, then save both to verify |
| 85 | # that each only updated the modified field. |
| 86 | >>> a1 = Article.objects.get(pk=a.id) |
| 87 | >>> a2 = Article.objects.get(pk=a.id) |
| 88 | >>> a1.headline = 'Changed article 1' |
| 89 | >>> a2.body = 'Changed body 1' |
| 90 | >>> a1.save() |
| 91 | >>> a2.save() |
| 92 | >>> a3 = Article.objects.get(pk=a.id) |
| 93 | >>> a3.headline |
| 94 | u'Changed article 1' |
| 95 | >>> a3.body |
| 96 | u'Changed body 1' |
| 97 | |
| 98 | # Fields entered at instantiation of a model which will already exists should be |
| 99 | # saved as well. |
| 100 | >>> a = Article(id=a.id, headline='Reset article 1', body='Reset body 1', pub_date=datetime(2007, 5, 5)) |
| 101 | >>> a.save() |
| 102 | >>> a3 = Article.objects.get(pk=a.id) |
| 103 | >>> a3.headline |
| 104 | u'Reset article 1' |
| 105 | >>> a3.body |
| 106 | u'Reset body 1' |
| 107 | |
| 108 | # If the value doesn't change, it won't be marked as modified -- otherwise user |
| 109 | # input like forms/etc is going to mark everything modified regardless. |
| 110 | >>> a1 = Article.objects.get(pk=a.id) |
| 111 | >>> a2 = Article.objects.get(pk=a.id) |
| 112 | >>> a1.headline = 'Changed article 2' |
| 113 | >>> a2.headline = a2.headline |
| 114 | >>> a2.body = 'Changed body 2' |
| 115 | >>> a1.save() |
| 116 | >>> a2.save() |
| 117 | >>> a3 = Article.objects.get(pk=a.id) |
| 118 | >>> a3.headline |
| 119 | u'Changed article 2' |
| 120 | >>> a3.body |
| 121 | u'Changed body 2' |
| 122 | """} |