models.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. Tests for the order_with_respect_to Meta attribute.
  3. """
  4. from django.db import models
  5. from django.utils import six
  6. from django.utils.encoding import python_2_unicode_compatible
  7. class Question(models.Model):
  8. text = models.CharField(max_length=200)
  9. @python_2_unicode_compatible
  10. class Answer(models.Model):
  11. text = models.CharField(max_length=200)
  12. question = models.ForeignKey(Question, models.CASCADE)
  13. class Meta:
  14. order_with_respect_to = 'question'
  15. def __str__(self):
  16. return six.text_type(self.text)
  17. @python_2_unicode_compatible
  18. class Post(models.Model):
  19. title = models.CharField(max_length=200)
  20. parent = models.ForeignKey("self", models.SET_NULL, related_name="children", null=True)
  21. class Meta:
  22. order_with_respect_to = "parent"
  23. def __str__(self):
  24. return self.title
  25. # order_with_respect_to points to a model with a OneToOneField primary key.
  26. class Entity(models.Model):
  27. pass
  28. class Dimension(models.Model):
  29. entity = models.OneToOneField('Entity', primary_key=True, on_delete=models.CASCADE)
  30. class Component(models.Model):
  31. dimension = models.ForeignKey('Dimension', on_delete=models.CASCADE)
  32. class Meta:
  33. order_with_respect_to = 'dimension'