| 1 |
""" |
| 2 |
Tests for defer() and only(). |
| 3 |
""" |
| 4 |
|
| 5 |
from django.db import models |
| 6 |
|
| 7 |
|
| 8 |
class Secondary(models.Model): |
| 9 |
first = models.CharField(max_length=50) |
| 10 |
second = models.CharField(max_length=50) |
| 11 |
|
| 12 |
|
| 13 |
class Primary(models.Model): |
| 14 |
name = models.CharField(max_length=50) |
| 15 |
value = models.CharField(max_length=50) |
| 16 |
related = models.ForeignKey(Secondary, models.CASCADE) |
| 17 |
|
| 18 |
def __str__(self): |
| 19 |
return self.name |
| 20 |
|
| 21 |
|
| 22 |
class PrimaryOneToOne(models.Model): |
| 23 |
name = models.CharField(max_length=50) |
| 24 |
value = models.CharField(max_length=50) |
| 25 |
related = models.OneToOneField( |
| 26 |
Secondary, models.CASCADE, related_name="primary_o2o" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
class Child(Primary): |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
class BigChild(Primary): |
| 35 |
other = models.CharField(max_length=50) |
| 36 |
|
| 37 |
|
| 38 |
class ChildProxy(Child): |
| 39 |
class Meta: |
| 40 |
proxy = True |
| 41 |
|
| 42 |
|
| 43 |
class RefreshPrimaryProxy(Primary): |
| 44 |
class Meta: |
| 45 |
proxy = True |
| 46 |
|
| 47 |
def refresh_from_db(self, using=None, fields=None, **kwargs): |
| 48 |
# Reloads all deferred fields if any of the fields is deferred. |
| 49 |
if fields is not None: |
| 50 |
fields = set(fields) |
| 51 |
deferred_fields = self.get_deferred_fields() |
| 52 |
if fields.intersection(deferred_fields): |
| 53 |
fields = fields.union(deferred_fields) |
| 54 |
super().refresh_from_db(using, fields, **kwargs) |
| 55 |
|
| 56 |
|
| 57 |
class ShadowParent(models.Model): |
| 58 |
""" |
| 59 |
ShadowParent declares a scalar, rather than a field. When this is |
| 60 |
overridden, the field value, rather than the scalar value must still be |
| 61 |
used when the field is deferred. |
| 62 |
""" |
| 63 |
|
| 64 |
name = "aphrodite" |
| 65 |
|
| 66 |
|
| 67 |
class ShadowChild(ShadowParent): |
| 68 |
name = models.CharField(default="adonis", max_length=6) |