models.py 1019 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. """
  2. 33. get_or_create()
  3. ``get_or_create()`` does what it says: it tries to look up an object with the
  4. given parameters. If an object isn't found, it creates one with the given
  5. parameters.
  6. """
  7. from __future__ import unicode_literals
  8. from django.db import models
  9. from django.utils.encoding import python_2_unicode_compatible
  10. @python_2_unicode_compatible
  11. class Person(models.Model):
  12. first_name = models.CharField(max_length=100)
  13. last_name = models.CharField(max_length=100)
  14. birthday = models.DateField()
  15. def __str__(self):
  16. return '%s %s' % (self.first_name, self.last_name)
  17. class ManualPrimaryKeyTest(models.Model):
  18. id = models.IntegerField(primary_key=True)
  19. data = models.CharField(max_length=100)
  20. class Profile(models.Model):
  21. person = models.ForeignKey(Person, primary_key=True)
  22. class Tag(models.Model):
  23. text = models.CharField(max_length=255, unique=True)
  24. class Thing(models.Model):
  25. name = models.CharField(max_length=256)
  26. tags = models.ManyToManyField(Tag)