2
0

models.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from django.db import models
  2. from wagtail.wagtailcore.models import Page
  3. from wagtail.wagtailcore.fields import StreamField
  4. from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
  5. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  6. from wagtail.wagtailsearch import index
  7. from wagtail.wagtailcore import blocks
  8. from wagtail.wagtailsnippets.models import register_snippet
  9. @register_snippet
  10. class Country(models.Model):
  11. '''
  12. Standard Django model to store set of countries of origin.
  13. Exposed in the Wagtail admin via Snippets.
  14. '''
  15. title = models.CharField(max_length=100)
  16. def __str__(self):
  17. return self.title
  18. class Meta:
  19. verbose_name_plural = "Countries of Origin"
  20. class BreadsLandingPage(Page):
  21. '''
  22. Home page for breads. Nothing needed in the model here - we'll just
  23. create an instance, then pull its children into the template.
  24. '''
  25. pass
  26. @register_snippet
  27. class BreadType(models.Model):
  28. '''
  29. Standard Django model used as a Snippet in the BreadPage model.
  30. '''
  31. title = models.CharField(max_length=255)
  32. panels = [
  33. FieldPanel('title'),
  34. ]
  35. def __str__(self):
  36. return self.title
  37. class Meta:
  38. verbose_name_plural = "Bread types"
  39. class BreadPage(Page):
  40. '''
  41. Detail view for a specific bread
  42. '''
  43. origin = models.ForeignKey(
  44. Country,
  45. on_delete=models.SET_NULL,
  46. null=True,
  47. blank=True,
  48. )
  49. description = StreamField([
  50. ('heading', blocks.CharBlock(classname="full title")),
  51. ('paragraph', blocks.RichTextBlock()),
  52. ])
  53. bread_type = models.ForeignKey(
  54. 'breads.BreadType',
  55. null=True,
  56. blank=True,
  57. on_delete=models.SET_NULL,
  58. related_name='+'
  59. )
  60. image = models.ForeignKey(
  61. 'wagtailimages.Image',
  62. on_delete=models.SET_NULL,
  63. null=True,
  64. blank=True,
  65. related_name='+',
  66. help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
  67. )
  68. content_panels = [
  69. FieldPanel('title'),
  70. FieldPanel('origin'),
  71. FieldPanel('bread_type'),
  72. ImageChooserPanel('image'),
  73. StreamFieldPanel('description'),
  74. ]
  75. search_fields = Page.search_fields + [
  76. index.SearchField('title'),
  77. index.SearchField('description'),
  78. ]