PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/model_formsets/models.py

https://code.google.com/p/mango-py/
Python | 193 lines | 131 code | 52 blank | 10 comment | 0 complexity | 759244b0d7824ead9b52f48baf14cc00 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import datetime
  2. from django.db import models
  3. class Author(models.Model):
  4. name = models.CharField(max_length=100)
  5. class Meta:
  6. ordering = ('name',)
  7. def __unicode__(self):
  8. return self.name
  9. class BetterAuthor(Author):
  10. write_speed = models.IntegerField()
  11. class Book(models.Model):
  12. author = models.ForeignKey(Author)
  13. title = models.CharField(max_length=100)
  14. class Meta:
  15. unique_together = (
  16. ('author', 'title'),
  17. )
  18. ordering = ['id']
  19. def __unicode__(self):
  20. return self.title
  21. class BookWithCustomPK(models.Model):
  22. my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
  23. author = models.ForeignKey(Author)
  24. title = models.CharField(max_length=100)
  25. def __unicode__(self):
  26. return u'%s: %s' % (self.my_pk, self.title)
  27. class Editor(models.Model):
  28. name = models.CharField(max_length=100)
  29. class BookWithOptionalAltEditor(models.Model):
  30. author = models.ForeignKey(Author)
  31. # Optional secondary author
  32. alt_editor = models.ForeignKey(Editor, blank=True, null=True)
  33. title = models.CharField(max_length=100)
  34. class Meta:
  35. unique_together = (
  36. ('author', 'title', 'alt_editor'),
  37. )
  38. def __unicode__(self):
  39. return self.title
  40. class AlternateBook(Book):
  41. notes = models.CharField(max_length=100)
  42. def __unicode__(self):
  43. return u'%s - %s' % (self.title, self.notes)
  44. class AuthorMeeting(models.Model):
  45. name = models.CharField(max_length=100)
  46. authors = models.ManyToManyField(Author)
  47. created = models.DateField(editable=False)
  48. def __unicode__(self):
  49. return self.name
  50. class CustomPrimaryKey(models.Model):
  51. my_pk = models.CharField(max_length=10, primary_key=True)
  52. some_field = models.CharField(max_length=100)
  53. # models for inheritance tests.
  54. class Place(models.Model):
  55. name = models.CharField(max_length=50)
  56. city = models.CharField(max_length=50)
  57. def __unicode__(self):
  58. return self.name
  59. class Owner(models.Model):
  60. auto_id = models.AutoField(primary_key=True)
  61. name = models.CharField(max_length=100)
  62. place = models.ForeignKey(Place)
  63. def __unicode__(self):
  64. return "%s at %s" % (self.name, self.place)
  65. class Location(models.Model):
  66. place = models.ForeignKey(Place, unique=True)
  67. # this is purely for testing the data doesn't matter here :)
  68. lat = models.CharField(max_length=100)
  69. lon = models.CharField(max_length=100)
  70. class OwnerProfile(models.Model):
  71. owner = models.OneToOneField(Owner, primary_key=True)
  72. age = models.PositiveIntegerField()
  73. def __unicode__(self):
  74. return "%s is %d" % (self.owner.name, self.age)
  75. class Restaurant(Place):
  76. serves_pizza = models.BooleanField()
  77. def __unicode__(self):
  78. return self.name
  79. class Product(models.Model):
  80. slug = models.SlugField(unique=True)
  81. def __unicode__(self):
  82. return self.slug
  83. class Price(models.Model):
  84. price = models.DecimalField(max_digits=10, decimal_places=2)
  85. quantity = models.PositiveIntegerField()
  86. def __unicode__(self):
  87. return u"%s for %s" % (self.quantity, self.price)
  88. class Meta:
  89. unique_together = (('price', 'quantity'),)
  90. class MexicanRestaurant(Restaurant):
  91. serves_tacos = models.BooleanField()
  92. class ClassyMexicanRestaurant(MexicanRestaurant):
  93. restaurant = models.OneToOneField(MexicanRestaurant, parent_link=True, primary_key=True)
  94. tacos_are_yummy = models.BooleanField()
  95. # models for testing unique_together validation when a fk is involved and
  96. # using inlineformset_factory.
  97. class Repository(models.Model):
  98. name = models.CharField(max_length=25)
  99. def __unicode__(self):
  100. return self.name
  101. class Revision(models.Model):
  102. repository = models.ForeignKey(Repository)
  103. revision = models.CharField(max_length=40)
  104. class Meta:
  105. unique_together = (("repository", "revision"),)
  106. def __unicode__(self):
  107. return u"%s (%s)" % (self.revision, unicode(self.repository))
  108. # models for testing callable defaults (see bug #7975). If you define a model
  109. # with a callable default value, you cannot rely on the initial value in a
  110. # form.
  111. class Person(models.Model):
  112. name = models.CharField(max_length=128)
  113. class Membership(models.Model):
  114. person = models.ForeignKey(Person)
  115. date_joined = models.DateTimeField(default=datetime.datetime.now)
  116. karma = models.IntegerField()
  117. # models for testing a null=True fk to a parent
  118. class Team(models.Model):
  119. name = models.CharField(max_length=100)
  120. class Player(models.Model):
  121. team = models.ForeignKey(Team, null=True)
  122. name = models.CharField(max_length=100)
  123. def __unicode__(self):
  124. return self.name
  125. # Models for testing custom ModelForm save methods in formsets and inline formsets
  126. class Poet(models.Model):
  127. name = models.CharField(max_length=100)
  128. def __unicode__(self):
  129. return self.name
  130. class Poem(models.Model):
  131. poet = models.ForeignKey(Poet)
  132. name = models.CharField(max_length=100)
  133. def __unicode__(self):
  134. return self.name
  135. class Post(models.Model):
  136. title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
  137. slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
  138. subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
  139. posted = models.DateField()
  140. def __unicode__(self):
  141. return self.name