models.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. Tests for defer() and only().
  3. """
  4. from django.db import models
  5. from django.utils.encoding import python_2_unicode_compatible
  6. class Secondary(models.Model):
  7. first = models.CharField(max_length=50)
  8. second = models.CharField(max_length=50)
  9. @python_2_unicode_compatible
  10. class Primary(models.Model):
  11. name = models.CharField(max_length=50)
  12. value = models.CharField(max_length=50)
  13. related = models.ForeignKey(Secondary)
  14. def __str__(self):
  15. return self.name
  16. class Child(Primary):
  17. pass
  18. class BigChild(Primary):
  19. other = models.CharField(max_length=50)
  20. class ChildProxy(Child):
  21. class Meta:
  22. proxy = True
  23. class RefreshPrimaryProxy(Primary):
  24. class Meta:
  25. proxy = True
  26. def refresh_from_db(self, using=None, fields=None, **kwargs):
  27. # Reloads all deferred fields if any of the fields is deferred.
  28. if fields is not None:
  29. fields = set(fields)
  30. deferred_fields = self.get_deferred_fields()
  31. if fields.intersection(deferred_fields):
  32. fields = fields.union(deferred_fields)
  33. super(RefreshPrimaryProxy, self).refresh_from_db(using, fields, **kwargs)