models.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. Many-to-one relationships
  3. To define a many-to-one relationship, use ``ForeignKey()``.
  4. """
  5. from __future__ import unicode_literals
  6. from django.db import models
  7. from django.utils.encoding import python_2_unicode_compatible
  8. @python_2_unicode_compatible
  9. class Reporter(models.Model):
  10. first_name = models.CharField(max_length=30)
  11. last_name = models.CharField(max_length=30)
  12. email = models.EmailField()
  13. def __str__(self):
  14. return "%s %s" % (self.first_name, self.last_name)
  15. @python_2_unicode_compatible
  16. class Article(models.Model):
  17. headline = models.CharField(max_length=100)
  18. pub_date = models.DateField()
  19. reporter = models.ForeignKey(Reporter, models.CASCADE)
  20. def __str__(self):
  21. return self.headline
  22. class Meta:
  23. ordering = ('headline',)
  24. # If ticket #1578 ever slips back in, these models will not be able to be
  25. # created (the field names being lower-cased versions of their opposite
  26. # classes is important here).
  27. class First(models.Model):
  28. second = models.IntegerField()
  29. class Second(models.Model):
  30. first = models.ForeignKey(First, models.CASCADE, related_name='the_first')
  31. # Protect against repetition of #1839, #2415 and #2536.
  32. class Third(models.Model):
  33. name = models.CharField(max_length=20)
  34. third = models.ForeignKey('self', models.SET_NULL, null=True, related_name='child_set')
  35. class Parent(models.Model):
  36. name = models.CharField(max_length=20, unique=True)
  37. bestchild = models.ForeignKey('Child', models.SET_NULL, null=True, related_name='favored_by')
  38. class Child(models.Model):
  39. name = models.CharField(max_length=20)
  40. parent = models.ForeignKey(Parent, models.CASCADE)
  41. class ToFieldChild(models.Model):
  42. parent = models.ForeignKey(Parent, models.CASCADE, to_field='name')
  43. # Multiple paths to the same model (#7110, #7125)
  44. @python_2_unicode_compatible
  45. class Category(models.Model):
  46. name = models.CharField(max_length=20)
  47. def __str__(self):
  48. return self.name
  49. class Record(models.Model):
  50. category = models.ForeignKey(Category, models.CASCADE)
  51. @python_2_unicode_compatible
  52. class Relation(models.Model):
  53. left = models.ForeignKey(Record, models.CASCADE, related_name='left_set')
  54. right = models.ForeignKey(Record, models.CASCADE, related_name='right_set')
  55. def __str__(self):
  56. return "%s - %s" % (self.left.category.name, self.right.category.name)
  57. # Test related objects visibility.
  58. class SchoolManager(models.Manager):
  59. def get_queryset(self):
  60. return super(SchoolManager, self).get_queryset().filter(is_public=True)
  61. class School(models.Model):
  62. is_public = models.BooleanField(default=False)
  63. objects = SchoolManager()
  64. class Student(models.Model):
  65. school = models.ForeignKey(School, models.CASCADE)