models.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from django.utils.encoding import python_2_unicode_compatible
  4. from django.utils.timezone import now
  5. #In order to run the database queries, there’s a manager for every model.
  6. # Unless it’s otherwise defined, the attribute objects holds a reference to the default manager.
  7. # The queries can be altered by overriding the default manager or adding another one.
  8. class PublicBookmarkManager(models.Manager):
  9. def get_queryset(self):
  10. qs = super(PublicBookmarkManager, self).get_queryset()
  11. return qs.filter(is_public=True)
  12. # Create your models here.
  13. # Models define how data is saved. Usually a model represents a table in the database, and has fields, metadata and methods
  14. @python_2_unicode_compatible
  15. class Tag(models.Model):
  16. name = models.CharField(max_length=50, unique=True)
  17. # Models can contain Metadata, that can influence how they are displayed or how they behave
  18. class Meta:
  19. verbose_name = 'tag'
  20. verbose_name_plural = 'tagS'
  21. ordering = ['name']
  22. # You can now add Model functionality Methods for actions that apply to a single record.
  23. # For instance, it is usual to create a human readable form of the record with the method __str__.
  24. def __str__(self):
  25. return self.name
  26. @python_2_unicode_compatible
  27. class Bookmark(models.Model):
  28. url = models.URLField()
  29. title = models.CharField('title', max_length=255)
  30. description = models.TextField('description', blank=True)
  31. is_public = models.BooleanField('public', default=True)
  32. date_created = models.DateTimeField('date created')
  33. date_updated = models.DateTimeField('date updated')
  34. owner = models.ForeignKey(User, verbose_name='owner', related_name='bookmarks', on_delete=models.DO_NOTHING)
  35. tags = models.ManyToManyField(Tag, blank=True)
  36. # In our example we’ll often show only public bookmarks so we add a second manager to the Bookmark model that will only return the public bookmarks.
  37. # We’ll assign it to the attribute public. In order to keep a reference to the default manager, we have to explicitely assign it to the objects
  38. object = models.Manager()
  39. public = PublicBookmarkManager()
  40. class Meta:
  41. verbose_name = 'bookmark'
  42. verbose_name_plural = 'bookmarkS'
  43. ordering = ['-date_created']
  44. def __str__(self):
  45. return '%s (%s)' % (self.title, self.url)
  46. #In the bookmark model, we’ll also override the method save() to set the right creation or last-changed date.
  47. # The state of the id field will be used, in order to decide if the model has already been saved or not.
  48. def save(self, *args, **kwargs):
  49. if not self.id:
  50. self.date_created =now()
  51. self.date_updated = now()
  52. super(Bookmark, self).save(*args, **kwargs)