models.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django.contrib.auth import models as auth
  2. from django.db import models
  3. from django.utils.encoding import python_2_unicode_compatible
  4. # No related name is needed here, since symmetrical relations are not
  5. # explicitly reversible.
  6. @python_2_unicode_compatible
  7. class SelfRefer(models.Model):
  8. name = models.CharField(max_length=10)
  9. references = models.ManyToManyField('self')
  10. related = models.ManyToManyField('self')
  11. def __str__(self):
  12. return self.name
  13. @python_2_unicode_compatible
  14. class Tag(models.Model):
  15. name = models.CharField(max_length=10)
  16. def __str__(self):
  17. return self.name
  18. # Regression for #11956 -- a many to many to the base class
  19. @python_2_unicode_compatible
  20. class TagCollection(Tag):
  21. tags = models.ManyToManyField(Tag, related_name='tag_collections')
  22. def __str__(self):
  23. return self.name
  24. # A related_name is required on one of the ManyToManyField entries here because
  25. # they are both addressable as reverse relations from Tag.
  26. @python_2_unicode_compatible
  27. class Entry(models.Model):
  28. name = models.CharField(max_length=10)
  29. topics = models.ManyToManyField(Tag)
  30. related = models.ManyToManyField(Tag, related_name="similar")
  31. def __str__(self):
  32. return self.name
  33. # Two models both inheriting from a base model with a self-referential m2m field
  34. class SelfReferChild(SelfRefer):
  35. pass
  36. class SelfReferChildSibling(SelfRefer):
  37. pass
  38. # Many-to-Many relation between models, where one of the PK's isn't an Autofield
  39. class Line(models.Model):
  40. name = models.CharField(max_length=100)
  41. class Worksheet(models.Model):
  42. id = models.CharField(primary_key=True, max_length=100)
  43. lines = models.ManyToManyField(Line, blank=True)
  44. # Regression for #11226 -- A model with the same name that another one to
  45. # which it has a m2m relation. This shouldn't cause a name clash between
  46. # the automatically created m2m intermediary table FK field names when
  47. # running migrate
  48. class User(models.Model):
  49. name = models.CharField(max_length=30)
  50. friends = models.ManyToManyField(auth.User)
  51. class BadModelWithSplit(models.Model):
  52. name = models.CharField(max_length=1)
  53. def split(self):
  54. raise RuntimeError('split should not be called')
  55. class Meta:
  56. abstract = True
  57. class RegressionModelSplit(BadModelWithSplit):
  58. """
  59. Model with a split method should not cause an error in add_lazy_relation
  60. """
  61. others = models.ManyToManyField('self')