Skip to content

Instantly share code, notes, and snippets.

@joshkehn
Created June 26, 2013 17:35
Show Gist options
  • Save joshkehn/5869518 to your computer and use it in GitHub Desktop.
Save joshkehn/5869518 to your computer and use it in GitHub Desktop.
Ingredient ModelForm (Django)
from django import forms
from menu.models import Ingredient, Diet, FoodPreference
class IngredientForm (forms.ModelForm):
class Meta:
model = Ingredient
exclude = ["franchise"]
def __init__ (self, *args, **kwargs):
brand = kwargs.pop("brand")
super(IngredientForm, self).__init__(*args, **kwargs)
self.fields["diets"].widget = forms.widgets.CheckboxSelectMultiple()
self.fields["diets"].help_text = ""
self.fields["diets"].queryset = Diet.objects.all()
self.fields["preferences"].widget = forms.widgets.CheckboxSelectMultiple()
self.fields["preferences"].help_text = ""
self.fields["preferences"].queryset = FoodPreference.objects.filter(franchise=brand)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment