Skip to content

Instantly share code, notes, and snippets.

@mekhami
Last active June 2, 2016 19:30
Show Gist options
  • Save mekhami/1db52cc201644eead3e628d6cee1eaf8 to your computer and use it in GitHub Desktop.
Save mekhami/1db52cc201644eead3e628d6cee1eaf8 to your computer and use it in GitHub Desktop.
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = ['book_title', 'customer', 'internal_design', 'paid', 'start_date']
campaign = forms.ModelMultipleChoiceField(queryset=Campaign.objects.all())
def save(self, commit=True):
campaign = Campaign.objects.get(pk=self.cleaned_data['campaign'])
# I need to create a Product object for each ProductType in campaign.producttypes.
# Product has a foreign key to Order. How do I create and associate these without
for producttype in campaign.product_types:
Product.objects.update_or_create(
order=self.instance,
product_type=producttype,
defaults=???)
return super().save(commit=commit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment