models.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from django.db import models
  2. from django.db.models import QuerySet
  3. from django.db.models.manager import BaseManager
  4. from django.urls import reverse
  5. from django.utils.encoding import python_2_unicode_compatible
  6. @python_2_unicode_compatible
  7. class Artist(models.Model):
  8. name = models.CharField(max_length=100)
  9. class Meta:
  10. ordering = ['name']
  11. verbose_name = 'professional artist'
  12. verbose_name_plural = 'professional artists'
  13. def __str__(self):
  14. return self.name
  15. def get_absolute_url(self):
  16. return reverse('artist_detail', kwargs={'pk': self.id})
  17. @python_2_unicode_compatible
  18. class Author(models.Model):
  19. name = models.CharField(max_length=100)
  20. slug = models.SlugField()
  21. class Meta:
  22. ordering = ['name']
  23. def __str__(self):
  24. return self.name
  25. class DoesNotExistQuerySet(QuerySet):
  26. def get(self, *args, **kwargs):
  27. raise Author.DoesNotExist
  28. DoesNotExistBookManager = BaseManager.from_queryset(DoesNotExistQuerySet)
  29. @python_2_unicode_compatible
  30. class Book(models.Model):
  31. name = models.CharField(max_length=300)
  32. slug = models.SlugField()
  33. pages = models.IntegerField()
  34. authors = models.ManyToManyField(Author)
  35. pubdate = models.DateField()
  36. objects = models.Manager()
  37. does_not_exist = DoesNotExistBookManager()
  38. class Meta:
  39. ordering = ['-pubdate']
  40. def __str__(self):
  41. return self.name
  42. class Page(models.Model):
  43. content = models.TextField()
  44. template = models.CharField(max_length=300)
  45. class BookSigning(models.Model):
  46. event_date = models.DateTimeField()