Skip to content

Commit

Permalink
Merge pull request wagtail-nest#70 from wagtail/64-bread-ingredients
Browse files Browse the repository at this point in the history
BreadIngredient <--> Bread M2M example
  • Loading branch information
daaray authored Feb 25, 2017
2 parents 1e71054 + 619ceb6 commit 4d74b41
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
10 changes: 7 additions & 3 deletions bakerydemo/base/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from wagtail.contrib.modeladmin.options import (
ModelAdmin, ModelAdminGroup, modeladmin_register)

from bakerydemo.breads.models import Country, BreadType
from bakerydemo.breads.models import Country, BreadIngredient, BreadType
from bakerydemo.base.models import People, FooterText

'''
Expand All @@ -21,9 +21,13 @@
'''


class BreadTypeAdmin(ModelAdmin):
class BreadIngredientAdmin(ModelAdmin):
# These stub classes allow us to put various models into the custom "Wagtail Bakery" menu item
# rather than under the default Snippets section.
model = BreadIngredient


class BreadTypeAdmin(ModelAdmin):
model = BreadType


Expand All @@ -35,7 +39,7 @@ class BreadModelAdminGroup(ModelAdminGroup):
menu_label = 'Bread Categories'
menu_icon = 'fa-suitcase' # change as required
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
items = (BreadTypeAdmin, BreadCountryAdmin)
items = (BreadIngredientAdmin, BreadTypeAdmin, BreadCountryAdmin)


class PeopleModelAdmin(ModelAdmin):
Expand Down
31 changes: 31 additions & 0 deletions bakerydemo/breads/migrations/0005_auto_20170224_1047.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-24 10:47
from __future__ import unicode_literals

from django.db import migrations, models
import modelcluster.fields


class Migration(migrations.Migration):

dependencies = [
('breads', '0004_auto_20170220_0111'),
]

operations = [
migrations.CreateModel(
name='BreadIngredient',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
],
options={
'verbose_name_plural': 'Bread ingredients',
},
),
migrations.AddField(
model_name='breadpage',
name='ingredients',
field=modelcluster.fields.ParentalManyToManyField(blank=True, to='breads.BreadIngredient'),
),
]
37 changes: 36 additions & 1 deletion bakerydemo/breads/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from django import forms
from django.db import models
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from modelcluster.fields import ParentalManyToManyField

from wagtail.wagtailadmin.edit_handlers import (
FieldPanel, MultiFieldPanel, StreamFieldPanel
)
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.models import Page

Expand All @@ -28,6 +33,25 @@ class Meta:
verbose_name_plural = "Countries of Origin"


@register_snippet
class BreadIngredient(models.Model):
"""
Standard Django model used as a Snippet in the BreadPage model.
Demonstrates ManyToMany relationship.
"""
name = models.CharField(max_length=255)

panels = [
FieldPanel('name'),
]

def __str__(self):
return self.name

class Meta:
verbose_name_plural = 'Bread ingredients'


@register_snippet
class BreadType(models.Model):
"""
Expand Down Expand Up @@ -68,11 +92,22 @@ class BreadPage(BasePageFieldsMixin, Page):
on_delete=models.SET_NULL,
related_name='+'
)
ingredients = ParentalManyToManyField('BreadIngredient', blank=True)

content_panels = BasePageFieldsMixin.content_panels + [
StreamFieldPanel('body'),
FieldPanel('origin'),
FieldPanel('bread_type'),
MultiFieldPanel(
[
FieldPanel(
'ingredients',
widget=forms.CheckboxSelectMultiple,
),
],
heading="Additional Metadata",
classname="collapsible collapsed"
),
]

search_fields = Page.search_fields + [
Expand Down
12 changes: 12 additions & 0 deletions bakerydemo/templates/breads/bread_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@ <h1>{{ page.title }}</h1>

<p>{{ page.origin }}</p>
<p>{{ page.bread_type }}</p>
{% with ingredients=page.ingredients.all %}
{% if ingredients %}
<h3>Ingredients:</h3>
<ul>
{% for ingredient in ingredients %}
<li style="display: inline">
{{ ingredient.name }}
</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{{ page.body }}
{% endblock content %}

0 comments on commit 4d74b41

Please sign in to comment.