Skip to content

Instantly share code, notes, and snippets.

@cyface
Created July 21, 2012 22:29

Revisions

  1. cyface revised this gist Jul 21, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions child_create.html
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    {% extends "base.html" %}

    {% block headtitle %}Step Create{% endblock %}

    {% block content %}
    <section title="Child Create" data-role="content">
    <h2>Child Create</h2>
  2. cyface revised this gist Jul 21, 2012. 2 changed files with 5 additions and 5 deletions.
    8 changes: 4 additions & 4 deletions forms.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    """Django form definitions for M2M with Through Example"""
    """Django M2M With Through Example Forms"""

    from models import WeaveStep, Step, Weave
    from django.forms import ModelForm, ModelChoiceField, HiddenInput, IntegerField, CharField
    from django.forms import ModelForm, HiddenInput, IntegerField, CharField
    from django.contrib.auth.models import User

    class ChildEditForm(ModelForm):
    @@ -13,14 +14,13 @@ class Meta():
    model = Child
    exclude = ['families',]


    def save(self, commit=True):

    child = super(ChildEditForm, self).save() # Save the child so we have an ID for the m2m

    family_slug = self.cleaned_data.get('family_slug')
    family = Family.objects.get(slug=family_slug)
    order = self.cleaned_data.get('order')

    FamilyChild.objects.create(family=family, child=child, order=order)

    return child
    2 changes: 1 addition & 1 deletion urls.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    """URL Definitions for M2M with Through Example"""
    """Django M2M With Through Example URLs"""

    from django.conf.urls import patterns, url

  3. cyface revised this gist Jul 21, 2012. 4 changed files with 30 additions and 2 deletions.
    18 changes: 18 additions & 0 deletions child_create.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    {% extends "base.html" %}

    {% block headtitle %}Step Create{% endblock %}

    {% block content %}
    <section title="Child Create" data-role="content">
    <h2>Child Create</h2>

    <form action="{% url child_create slug=form.family_slug.value %}" method="POST">
    {{ form.errors }}
    {% csrf_token %}
    <ul>
    {{ form.as_ul }}
    </ul>
    <input type="submit" name="Submit" value="Submit">
    </form>
    </section>
    {% endblock %}
    2 changes: 1 addition & 1 deletion forms.py
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ class Meta():
    def save(self, commit=True):

    child = super(ChildEditForm, self).save() # Save the child so we have an ID for the m2m
    family_slug = self.cleaned_data.get('weave_slug')
    family_slug = self.cleaned_data.get('family_slug')
    family = Family.objects.get(slug=family_slug)
    order = self.cleaned_data.get('order')

    10 changes: 10 additions & 0 deletions urls.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    """URL Definitions for M2M with Through Example"""

    from django.conf.urls import patterns, url

    from views import ChildAdd

    urlpatterns = patterns('',
    # Child Create
    url(r'^family/(?P<slug>[-_\w]+)/add/child/$', ChildAdd.as_view(), name="child_create"),
    )
    2 changes: 1 addition & 1 deletion views.py
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,4 @@ def get_initial(self):
    family_slug = self.kwargs.get('slug', self.request.POST.get('slug'))
    family = get_object_or_404(Family, slug=family_slug)

    return {'weave_slug': weave_slug}
    return {'family_slug': family_slug}
  4. cyface revised this gist Jul 21, 2012. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions views.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    """Django M2M With Through Example Views"""

    from django.views.generic import CreateView
    from models import Child, Family
    from forms import ChildEditForm

    class ChildAdd(CreateView):
    """
    View to add a child to a family
    url: /family/<family-slug>/add/child
    """
    model = Child
    form_class = ChildEditForm
    context_object_name = 'child'
    template_name = 'child_create.html'

    def get_initial(self):
    """Calculate Initial Data for the form, validate ownership of family"""
    family_slug = self.kwargs.get('slug', self.request.POST.get('slug'))
    family = get_object_or_404(Family, slug=family_slug)

    return {'weave_slug': weave_slug}
  5. cyface revised this gist Jul 21, 2012. 2 changed files with 27 additions and 1 deletion.
    26 changes: 26 additions & 0 deletions forms.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    """Django form definitions for M2M with Through Example"""
    from models import WeaveStep, Step, Weave
    from django.forms import ModelForm, ModelChoiceField, HiddenInput, IntegerField, CharField
    from django.contrib.auth.models import User

    class ChildEditForm(ModelForm):
    """Form to Edit a Child and Specify the Ordering and Relation to Family"""

    family_slug = CharField(widget=HiddenInput()) # Pointer Back to Family
    order = IntegerField(required=False, widget=HiddenInput(), initial=0)

    class Meta():
    model = Child
    exclude = ['families',]


    def save(self, commit=True):

    child = super(ChildEditForm, self).save() # Save the child so we have an ID for the m2m
    family_slug = self.cleaned_data.get('weave_slug')
    family = Family.objects.get(slug=family_slug)
    order = self.cleaned_data.get('order')

    FamilyChild.objects.create(family=family, child=child, order=order)

    return child
    2 changes: 1 addition & 1 deletion models.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ class Child(models.Model):

    class Family(models.Model):
    """Families are groups of children in a particular order."""
    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)
    children = models.ManyToManyField('Child', through='FamilyChild')

    class FamilyChild(models.Model):
  6. cyface renamed this gist Jul 21, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. cyface created this gist Jul 21, 2012.
    20 changes: 20 additions & 0 deletions Models
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    """Django M2M With Through Example Models"""

    import datetime
    from django.db import models

    class Child(models.Model):
    """Each Child can have multiple families."""
    name = models.CharField(max_length=120)
    families = models.ManyToManyField('Parent', through='FamilyChild')

    class Family(models.Model):
    """Families are groups of children in a particular order."""
    name = models.CharField(max_length=255)
    children = models.ManyToManyField('Child', through='FamilyChild')

    class FamilyChild(models.Model):
    """A FamilyChild is the many-to-many intersection of Families and Children"""
    child = models.ForeignKey('Child', related_name='children')
    family = models.ForeignKey('Family', related_name='families')
    order = models.IntegerField(default=0)