Created
July 21, 2012 22:29
Revisions
-
cyface revised this gist
Jul 21, 2012 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,5 @@ {% extends "base.html" %} {% block content %} <section title="Child Create" data-role="content"> <h2>Child Create</h2> -
cyface revised this gist
Jul 21, 2012 . 2 changed files with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ """Django M2M With Through Example Forms""" from models import WeaveStep, Step, Weave 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ """Django M2M With Through Example URLs""" from django.conf.urls import patterns, url -
cyface revised this gist
Jul 21, 2012 . 4 changed files with 30 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 %} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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('family_slug') family = Family.objects.get(slug=family_slug) order = self.cleaned_data.get('order') This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"), ) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 {'family_slug': family_slug} -
cyface revised this gist
Jul 21, 2012 . 1 changed file with 22 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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} -
cyface revised this gist
Jul 21, 2012 . 2 changed files with 27 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.""" slug = models.SlugField(max_length=255) children = models.ManyToManyField('Child', through='FamilyChild') class FamilyChild(models.Model): -
cyface renamed this gist
Jul 21, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cyface created this gist
Jul 21, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)