Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble with my views and formsets saving
    primarykey
    data
    text
    <p>I am having the hardest time with my views and formsets saving. I have come to realize that views are my weakness in Django. I have been working on this for the past few months and writing and rewriting the code. I know this is the best way to learn. Luckily my team gave me this project to learn from. But I still don't know where I am going wrong.</p> <p>If anyone here sees where I am off or pointing me into the right direction, I'd greatly appreciate it. I'm searching for best resources to get stronger in this area. </p> <p>Here is my Views:</p> <pre><code>from django.conf import settings from django.views.generic import View from django.views.generic import TemplateView from django.shortcuts import render from itertools import chain from .models import * from .forms import * from .emailtemplates import OrderFormNotification class OrderFormProcessor(object): def __init__(self, contact=None, contact_form_class=ContactForm, letterhead_form_class=LetterHeadFormSet, windowenv_form_class=WindowEnvFormSet, numbertenenv_form_class=NumberTenEnvFormSet, ninebytwelveenv_form_class=NineByTwelveEnvFormSet, tenbythirteenenv_form_class=TenByThirteenEnvFormSet, businesscard_form_class=BusinessCardFormSet, send_notification=False): self.contact_form_class = contact_form_class self.letterhead_form_class = letterhead_form_class self.windowenv_form_class = windowenv_form_class self.numbertenenv_form_class = numbertenenv_form_class self.ninebytwelveenv_form_class = ninebytwelveenv_form_class self.tenbythirteenenv_form_class = tenbythirteenenv_form_class self.businesscard_form_class = businesscard_form_class self.send_notification = send_notification self.contact = contact def process(self, request, context=None): if not context: context = {} contact = self.contact data = request.POST or None contact_form = self.contact_form_class(data, instance=contact) forms = dict( contact_form=contact_form, ) formsets = dict( letterhead_formset=self.letterhead_form_class(data, prefix='letterhead_'), windowenv_formset=self.windowenv_form_class(data, prefix='windowenv_'), numbertenenv_formset=self.numbertenenv_form_class(data, prefix='numbertenenv_'), ninebytwelveenv_formset=self.ninebytwelveenv_form_class(data, prefix='ninebytwelveenv_'), tenbythirteenenv_formset=self.tenbythirteenenv_form_class(data, prefix='tenbythirteenenv_'), businesscard_formset=self.businesscard_form_class(data, prefix='businesscard_'), ) if request.method == 'POST': form_is_valid = all([f.is_valid() for f in forms.values() + \ formsets.values()]) if form_is_valid: contact = forms['contact_form'].save(commit=False) contact.letterhead_form = formsets['letterhead_formset'].save() contact.windowenv_form = formsets['windowenv_formset'].save() contact.numbertenenv_form = formsets['numbertenenv_formset'].save() contact.ninebytwelveenv_form = formsets['ninebytwelveenv_formset'].save() contact.tenbythirteenenv_form = formsets['tenbythirteenenv_formset'].save() contact.businesscard_form = formsets['businesscard_formset'].save() contact.save() if self.send_notification: email = OrderFormNotification(to=[settings.NO_REPLY_EMAIL_ADDRESS], extra_context=data) email.send() else: pass all_forms = chain(forms.values(), chain.from_iterable(formsets.values())) context = context.copy() context.update(forms.items() + formsets.items()) context.update( error_list=list(chain.from_iterable([form.errors.values() for form in all_forms])), ) return (forms, formsets), context class OrderFormView(View): template_name = 'orderform.html' def dispatch(self, request, *args, **kwargs): processor = OrderFormProcessor(send_notification=True) (forms, formsets), context = processor.process(request) return render(request, self.template_name, context) class ThankYou(TemplateView): template_name = 'thank-you.html' </code></pre> <p>Here are my Forms:</p> <pre><code>from django import forms from django.forms import ModelForm, extras from django.forms.models import modelformset_factory from django.contrib.localflavor import us from django.forms.widgets import RadioSelect, CheckboxSelectMultiple from .models import * class ContactForm(forms.ModelForm): address = forms.ChoiceField(required = True, widget=RadioSelect(), choices=ADDRESS_CHOICES,) class Meta: model = Contact class LetterHeadForm(forms.ModelForm): address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES) class Meta: model = LetterHead widgets = { 'contact': forms.HiddenInput, 'quantity': forms.Select(choices=QUANTITY_CHOICES), } class WindowEnvForm(forms.ModelForm): address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES) class Meta: model = WindowEnv widgets = { 'contact': forms.HiddenInput, 'quantity': forms.Select(choices=QUANTITY_CHOICES), } class NumberTenEnvForm(forms.ModelForm): address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES) class Meta: model = NumberTenEnv widgets = { 'contact': forms.HiddenInput, 'quantity': forms.Select(choices=QUANTITY_CHOICES), } class NineByTwelveEnvForm(forms.ModelForm): address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES) class Meta: model = NineByTwelveEnv widgets = { 'contact': forms.HiddenInput, 'quantity': forms.Select(choices=QUANTITY_CHOICES), } class TenByThirteenEnvForm(forms.ModelForm): address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES) class Meta: model = TenByThirteenEnv widgets = { 'contact': forms.HiddenInput, 'quantity': forms.Select(choices=QUANTITY_CHOICES), } class BusinessCardForm(forms.ModelForm): print_choices = forms.ChoiceField(required = True, widget=RadioSelect(), choices=PRINT_CHOICES) card_styles = forms.ChoiceField(required = True, widget=RadioSelect(), choices=CARD_CHOICES) card_mailing_address = forms.ChoiceField(required = True, widget=RadioSelect(), choices=ADDRESS_CHOICES) class Meta: model = BusinessCard widgets = { 'contact': forms.HiddenInput, } class RushOrderForm(forms.ModelForm): class Meta: model = RushOrder widgets = { 'contact': forms.HiddenInput, 'rush_order': forms.CheckboxInput, 'in_hand_date': forms.extras.SelectDateWidget } class OrderNoteForm(forms.ModelForm): class Meta: model = OrderNote widgets = { 'contact': forms.HiddenInput, 'add_note': forms.CheckboxInput, 'notes': forms.Textarea } LetterHeadFormSet = modelformset_factory(LetterHead, form=LetterHeadForm, extra=2, max_num=2) WindowEnvFormSet = modelformset_factory(WindowEnv, form=WindowEnvForm, extra=2, max_num=2) NumberTenEnvFormSet = modelformset_factory(NumberTenEnv, form=NumberTenEnvForm, extra=2, max_num=2) NineByTwelveEnvFormSet = modelformset_factory(NineByTwelveEnv, form=NineByTwelveEnvForm, extra=2, max_num=2) TenByThirteenEnvFormSet = modelformset_factory(TenByThirteenEnv, form=TenByThirteenEnvForm, extra=2, max_num=2) BusinessCardFormSet = modelformset_factory(BusinessCard, form=BusinessCardForm, extra=2, max_num=2) </code></pre> <p>Here are the Models:</p> <pre><code>from django.db import models from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ from django.contrib.localflavor.us.models import PhoneNumberField PRINT_CHOICES = ( ('exact reprint', 'Exact Reprint'), ('reprint with changes', 'Reprint With Changes'), ('new', 'New') ) QUANTITY_CHOICES = ( ('1000', '1000'), ('2500', '2500'), ('5000', '5000') ) CARD_QUANTITY_CHOICES = ( ('250', '250'), ('500', '500'), ('1000', '1000') ) CARD_CHOICES = ( ('chef/black', 'Chef/Black'), ('executive/red', 'Executive/Red') ) ADDRESS_CHOICES = ( ('eisenhower', 'Eisenhower'), ('wheeler', 'Wheeler'), ) class Contact(models.Model): first_name = models.CharField(max_length=100, help_text="First Name") last_name = models.CharField(max_length=100, help_text="Last Name") email_address = models.EmailField(max_length=275) address = models.CharField(max_length=10, choices=ADDRESS_CHOICES) def __unicode__(self): return "%s %s" % (self.first_name, self.last_name) class BaseStationary(models.Model): contact = models.ForeignKey(Contact, related_name='%(class)s_related') address = models.CharField(max_length=10, choices=ADDRESS_CHOICES) quantity = models.CharField(max_length=4, choices=QUANTITY_CHOICES) class Meta: abstract = True class LetterHead(BaseStationary): pass class WindowEnv(BaseStationary): pass class NumberTenEnv(BaseStationary): pass class NineByTwelveEnv(BaseStationary): pass class TenByThirteenEnv(BaseStationary): pass class BusinessCard(models.Model): contact = models.ForeignKey(Contact, related_name='businesscards') card_first_name = models.CharField(max_length=100) card_last_name = models.CharField(max_length=100) title = models.CharField(max_length=100) print_choices = models.CharField(max_length=19, choices=PRINT_CHOICES) card_styles = models.CharField(max_length=12, choices=CARD_CHOICES) card_email_address = models.EmailField(max_length=275) office_phone_number = PhoneNumberField(_('main office phone number'), blank=True, null=True) toll_free_number = PhoneNumberField(_('toll free number'), blank=True, null=True) mobile_number = PhoneNumberField(_('mobile phone number'), blank=True, null=True) fax_number = PhoneNumberField(_('main office fax'), blank=True, null=True) card_mailing_address = models.CharField(max_length=10, choices=ADDRESS_CHOICES) card_quantity = models.CharField(max_length=3, choices=CARD_QUANTITY_CHOICES) class RushOrder(models.Model): contact = models.ForeignKey(Contact, related_name='rushorders') rush_order = models.BooleanField() in_hand_date = models.DateField(blank=True, null=True) class OrderNote(models.Model): contact = models.ForeignKey(Contact, related_name='ordernotes') add_note = models.BooleanField() notes = models.TextField() </code></pre> <p>So again what I'm trying to do is get these formsets to save to the database, that way the admin will have the stationary order saved for printing purposes. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload