models.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. Tests for field subclassing.
  3. """
  4. import warnings
  5. from django.db import models
  6. from django.utils.encoding import force_text
  7. from django.utils.deprecation import RemovedInDjango20Warning
  8. from .fields import Small, SmallField, SmallerField, JSONField
  9. from django.utils.encoding import python_2_unicode_compatible
  10. # Catch warning about subfieldbase -- remove in Django 2.0
  11. warnings.filterwarnings(
  12. 'ignore',
  13. 'SubfieldBase has been deprecated. Use Field.from_db_value instead.',
  14. RemovedInDjango20Warning
  15. )
  16. @python_2_unicode_compatible
  17. class MyModel(models.Model):
  18. name = models.CharField(max_length=10)
  19. data = SmallField('small field')
  20. def __str__(self):
  21. return force_text(self.name)
  22. class OtherModel(models.Model):
  23. data = SmallerField()
  24. class ChoicesModel(models.Model):
  25. SMALL_AB = Small('a', 'b')
  26. SMALL_CD = Small('c', 'd')
  27. SMALL_CHOICES = (
  28. (SMALL_AB, str(SMALL_AB)),
  29. (SMALL_CD, str(SMALL_CD)),
  30. )
  31. data = SmallField('small field', choices=SMALL_CHOICES)
  32. class DataModel(models.Model):
  33. data = JSONField()