2009-03-19 10:06:04 +01:00
|
|
|
"""
|
|
|
|
Tests for defer() and only().
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2010-09-12 22:03:20 +02:00
|
|
|
|
2009-03-19 10:06:04 +01:00
|
|
|
|
|
|
|
class Secondary(models.Model):
|
|
|
|
first = models.CharField(max_length=50)
|
|
|
|
second = models.CharField(max_length=50)
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2009-03-19 10:06:04 +01:00
|
|
|
class Primary(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
value = models.CharField(max_length=50)
|
2015-07-22 16:43:21 +02:00
|
|
|
related = models.ForeignKey(Secondary, models.CASCADE)
|
2009-03-19 10:06:04 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2009-03-19 23:46:28 +01:00
|
|
|
return self.name
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2009-06-06 08:14:05 +02:00
|
|
|
class Child(Primary):
|
|
|
|
pass
|
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2009-06-06 08:14:05 +02:00
|
|
|
class BigChild(Primary):
|
|
|
|
other = models.CharField(max_length=50)
|
2012-03-12 23:33:18 +01:00
|
|
|
|
2013-11-02 23:50:35 +01:00
|
|
|
|
2012-03-12 23:33:18 +01:00
|
|
|
class ChildProxy(Child):
|
|
|
|
class Meta:
|
2013-10-23 12:09:29 +02:00
|
|
|
proxy = True
|
2014-07-05 08:03:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RefreshPrimaryProxy(Primary):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
def refresh_from_db(self, using=None, fields=None, **kwargs):
|
|
|
|
# Reloads all deferred fields if any of the fields is deferred.
|
|
|
|
if fields is not None:
|
|
|
|
fields = set(fields)
|
|
|
|
deferred_fields = self.get_deferred_fields()
|
|
|
|
if fields.intersection(deferred_fields):
|
|
|
|
fields = fields.union(deferred_fields)
|
2017-01-21 14:13:44 +01:00
|
|
|
super().refresh_from_db(using, fields, **kwargs)
|
2021-06-09 16:55:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ShadowParent(models.Model):
|
|
|
|
"""
|
|
|
|
ShadowParent declares a scalar, rather than a field. When this is
|
|
|
|
overridden, the field value, rather than the scalar value must still be
|
|
|
|
used when the field is deferred.
|
|
|
|
"""
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2021-06-09 16:55:22 +02:00
|
|
|
name = "aphrodite"
|
|
|
|
|
|
|
|
|
|
|
|
class ShadowChild(ShadowParent):
|
|
|
|
name = models.CharField(default="adonis", max_length=6)
|