models.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """
  2. The lookup API
  3. This demonstrates features of the database API.
  4. """
  5. from django.db import models
  6. from django.db.models.lookups import IsNull
  7. class Alarm(models.Model):
  8. desc = models.CharField(max_length=100)
  9. time = models.TimeField()
  10. def __str__(self):
  11. return '%s (%s)' % (self.time, self.desc)
  12. class Author(models.Model):
  13. name = models.CharField(max_length=100)
  14. alias = models.CharField(max_length=50, null=True, blank=True)
  15. class Meta:
  16. ordering = ('name',)
  17. class Article(models.Model):
  18. headline = models.CharField(max_length=100)
  19. pub_date = models.DateTimeField()
  20. author = models.ForeignKey(Author, models.SET_NULL, blank=True, null=True)
  21. slug = models.SlugField(unique=True, blank=True, null=True)
  22. class Meta:
  23. ordering = ('-pub_date', 'headline')
  24. def __str__(self):
  25. return self.headline
  26. class Tag(models.Model):
  27. articles = models.ManyToManyField(Article)
  28. name = models.CharField(max_length=100)
  29. class Meta:
  30. ordering = ('name',)
  31. class NulledTextField(models.TextField):
  32. def get_prep_value(self, value):
  33. return None if value == '' else value
  34. @NulledTextField.register_lookup
  35. class NulledTransform(models.Transform):
  36. lookup_name = 'nulled'
  37. template = 'NULL'
  38. @NulledTextField.register_lookup
  39. class IsNullWithNoneAsRHS(IsNull):
  40. lookup_name = 'isnull_none_rhs'
  41. can_use_none_as_rhs = True
  42. class Season(models.Model):
  43. year = models.PositiveSmallIntegerField()
  44. gt = models.IntegerField(null=True, blank=True)
  45. nulled_text_field = NulledTextField(null=True)
  46. class Meta:
  47. constraints = [
  48. models.UniqueConstraint(fields=['year'], name='season_year_unique'),
  49. ]
  50. def __str__(self):
  51. return str(self.year)
  52. class Game(models.Model):
  53. season = models.ForeignKey(Season, models.CASCADE, related_name='games')
  54. home = models.CharField(max_length=100)
  55. away = models.CharField(max_length=100)
  56. class Player(models.Model):
  57. name = models.CharField(max_length=100)
  58. games = models.ManyToManyField(Game, related_name='players')
  59. class Product(models.Model):
  60. name = models.CharField(max_length=80)
  61. qty_target = models.DecimalField(max_digits=6, decimal_places=2)
  62. class Stock(models.Model):
  63. product = models.ForeignKey(Product, models.CASCADE)
  64. qty_available = models.DecimalField(max_digits=6, decimal_places=2)
  65. class Freebie(models.Model):
  66. gift_product = models.ForeignKey(Product, models.CASCADE)
  67. stock_id = models.IntegerField(blank=True, null=True)
  68. stock = models.ForeignObject(
  69. Stock,
  70. from_fields=['stock_id', 'gift_product'],
  71. to_fields=['id', 'product'],
  72. on_delete=models.CASCADE,
  73. )