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 Child(Primary):
    
  23.     pass
    
  24. 
    
  25. 
    
  26. class BigChild(Primary):
    
  27.     other = models.CharField(max_length=50)
    
  28. 
    
  29. 
    
  30. class ChildProxy(Child):
    
  31.     class Meta:
    
  32.         proxy = True
    
  33. 
    
  34. 
    
  35. class RefreshPrimaryProxy(Primary):
    
  36.     class Meta:
    
  37.         proxy = True
    
  38. 
    
  39.     def refresh_from_db(self, using=None, fields=None, **kwargs):
    
  40.         # Reloads all deferred fields if any of the fields is deferred.
    
  41.         if fields is not None:
    
  42.             fields = set(fields)
    
  43.             deferred_fields = self.get_deferred_fields()
    
  44.             if fields.intersection(deferred_fields):
    
  45.                 fields = fields.union(deferred_fields)
    
  46.         super().refresh_from_db(using, fields, **kwargs)
    
  47. 
    
  48. 
    
  49. class ShadowParent(models.Model):
    
  50.     """
    
  51.     ShadowParent declares a scalar, rather than a field. When this is
    
  52.     overridden, the field value, rather than the scalar value must still be
    
  53.     used when the field is deferred.
    
  54.     """
    
  55. 
    
  56.     name = "aphrodite"
    
  57. 
    
  58. 
    
  59. class ShadowChild(ShadowParent):
    
  60.     name = models.CharField(default="adonis", max_length=6)