PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/m2m_through/models.py

https://code.google.com/p/mango-py/
Python | 65 lines | 48 code | 16 blank | 1 comment | 0 complexity | 33f517faac2e91cbd941ca96028b2d6e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.db import models
  2. from datetime import datetime
  3. # M2M described on one of the models
  4. class Person(models.Model):
  5. name = models.CharField(max_length=128)
  6. class Meta:
  7. ordering = ('name',)
  8. def __unicode__(self):
  9. return self.name
  10. class Group(models.Model):
  11. name = models.CharField(max_length=128)
  12. members = models.ManyToManyField(Person, through='Membership')
  13. custom_members = models.ManyToManyField(Person, through='CustomMembership', related_name="custom")
  14. nodefaultsnonulls = models.ManyToManyField(Person, through='TestNoDefaultsOrNulls', related_name="testnodefaultsnonulls")
  15. class Meta:
  16. ordering = ('name',)
  17. def __unicode__(self):
  18. return self.name
  19. class Membership(models.Model):
  20. person = models.ForeignKey(Person)
  21. group = models.ForeignKey(Group)
  22. date_joined = models.DateTimeField(default=datetime.now)
  23. invite_reason = models.CharField(max_length=64, null=True)
  24. class Meta:
  25. ordering = ('date_joined', 'invite_reason', 'group')
  26. def __unicode__(self):
  27. return "%s is a member of %s" % (self.person.name, self.group.name)
  28. class CustomMembership(models.Model):
  29. person = models.ForeignKey(Person, db_column="custom_person_column", related_name="custom_person_related_name")
  30. group = models.ForeignKey(Group)
  31. weird_fk = models.ForeignKey(Membership, null=True)
  32. date_joined = models.DateTimeField(default=datetime.now)
  33. def __unicode__(self):
  34. return "%s is a member of %s" % (self.person.name, self.group.name)
  35. class Meta:
  36. db_table = "test_table"
  37. class TestNoDefaultsOrNulls(models.Model):
  38. person = models.ForeignKey(Person)
  39. group = models.ForeignKey(Group)
  40. nodefaultnonull = models.CharField(max_length=5)
  41. class PersonSelfRefM2M(models.Model):
  42. name = models.CharField(max_length=5)
  43. friends = models.ManyToManyField('self', through="Friendship", symmetrical=False)
  44. def __unicode__(self):
  45. return self.name
  46. class Friendship(models.Model):
  47. first = models.ForeignKey(PersonSelfRefM2M, related_name="rel_from_set")
  48. second = models.ForeignKey(PersonSelfRefM2M, related_name="rel_to_set")
  49. date_friended = models.DateTimeField()