models.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.db import models
  4. from django.utils.encoding import python_2_unicode_compatible
  5. CHOICES = (
  6. (1, 'first'),
  7. (2, 'second'),
  8. )
  9. @python_2_unicode_compatible
  10. class Article(models.Model):
  11. headline = models.CharField(max_length=100, default='Default headline')
  12. pub_date = models.DateTimeField()
  13. status = models.IntegerField(blank=True, null=True, choices=CHOICES)
  14. misc_data = models.CharField(max_length=100, blank=True)
  15. article_text = models.TextField()
  16. class Meta:
  17. ordering = ('pub_date', 'headline')
  18. # A utf-8 verbose name (Ångström's Articles) to test they are valid.
  19. verbose_name = "\xc3\x85ngstr\xc3\xb6m's Articles"
  20. def __str__(self):
  21. return self.headline
  22. class Movie(models.Model):
  23. #5218: Test models with non-default primary keys / AutoFields
  24. movie_id = models.AutoField(primary_key=True)
  25. name = models.CharField(max_length=60)
  26. class Party(models.Model):
  27. when = models.DateField(null=True)
  28. class Event(models.Model):
  29. when = models.DateTimeField()
  30. @python_2_unicode_compatible
  31. class Department(models.Model):
  32. id = models.PositiveIntegerField(primary_key=True)
  33. name = models.CharField(max_length=200)
  34. def __str__(self):
  35. return self.name
  36. @python_2_unicode_compatible
  37. class Worker(models.Model):
  38. department = models.ForeignKey(Department)
  39. name = models.CharField(max_length=200)
  40. def __str__(self):
  41. return self.name
  42. @python_2_unicode_compatible
  43. class BrokenUnicodeMethod(models.Model):
  44. name = models.CharField(max_length=7)
  45. def __str__(self):
  46. # Intentionally broken (invalid start byte in byte string).
  47. return b'Name\xff: %s'.decode() % self.name
  48. class NonAutoPK(models.Model):
  49. name = models.CharField(max_length=10, primary_key=True)
  50. #18432: Chained foreign keys with to_field produce incorrect query
  51. class Model1(models.Model):
  52. pkey = models.IntegerField(unique=True, db_index=True)
  53. class Model2(models.Model):
  54. model1 = models.ForeignKey(Model1, unique=True, to_field='pkey')
  55. class Model3(models.Model):
  56. model2 = models.ForeignKey(Model2, unique=True, to_field='model1')