Noobie with no understanding of formsets - django

I have an instance where I am creating an order form with multiple models. I want these models to be rendered in one form and was told that formsets are my answer. I have been doing research how this works and still spinning my wheels not knowing. Sorry if this is simple and I'm not seeing it.
Here are my models:
class Contact(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
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=3, 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()
Here are my forms:
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 = True, widget=RadioSelect(attrs={'id': 'letterhead_address', 'required': 'True'}), choices=ADDRESS_CHOICES)
class Meta:
model = LetterHead
widgets = {
'contact': forms.HiddenInput,
'quantity': forms.Select(attrs={'id': 'letterhead_quantity'}, choices=QUANTITY_CHOICES),
}
class WindowEnvForm(forms.ModelForm):
address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'windowenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES)
class Meta:
model = WindowEnv
widgets = {
'contact': forms.HiddenInput,
'quantity': forms.Select(attrs={'id': 'windowenv_quantity'}, choices=QUANTITY_CHOICES),
}
class NumberTenEnvForm(forms.ModelForm):
address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'numbertenenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES)
class Meta:
model = NumberTenEnv
widgets = {
'contact': forms.HiddenInput,
'quantity': forms.Select(attrs={'id': 'numbertenenv_quantity'}, choices=QUANTITY_CHOICES),
}
class NineByTwelveEnvForm(forms.ModelForm):
address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'ninebytwelveenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES)
class Meta:
model = NineByTwelveEnv
widgets = {
'contact': forms.HiddenInput,
'quantity': forms.Select(attrs={'id': 'ninebytwelveenv_quantity'}, choices=QUANTITY_CHOICES),
}
class TenByThirteenEnvForm(forms.ModelForm):
address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'tenbythirteenenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES)
class Meta:
model = TenByThirteenEnv
widgets = {
'contact': forms.HiddenInput,
'quantity': forms.Select(attrs={'id': 'tenbythirteenenv_quantity'}, 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
}
And here is my view:
class OrderFormView(CreateView):
model = Contact
form_class = ContactForm
template_name = 'orderform.html'
success_url = 'success'
def get_context_data(self, **kwargs):
context = super(OrderFormView, self).get_context_data(**kwargs)
context.update({
'letterhead_form': LetterHeadForm,
'windowenv_form': WindowEnvForm,
'numbertenenv_form': NumberTenEnvForm,
'ninebytwelveenv_form': NineByTwelveEnvForm,
'tenbythirteenenv_form': TenByThirteenEnvForm,
'businesscard_form': BusinessCardForm,
'rushorder_form': RushOrderForm,
'ordernote_form': OrderNoteForm,
})
return context
def form_valid(self, form):
if form.is_valid():
data = form.cleaned_data
email = OrderFormNotification(to=[settings.ORDERFORM_EMAIL_ADDRESS, ],
extra_context=data)
email.send()
Thanks in advance for any insight. Even if it's to point me in the direction to better understand formsets for this.

If you need 9 forms for 9 different models then I don't believe formsets will help you. Formsets are for constructing multiple forms of the same type. Likewise the CreateView is intended to be used only in the simple case of creating a single model. If you are creating multiple models/validating multiple forms you will find yourself fighting with CreateView to make this work. You would be better off writing your own view class built from ProcessFormView perhaps even View.

Related

How to get a non-pk value to be the value by which a post request is made in django rest framework

As the question states, I'd like to be able to instead of having to pass a PK inside my JSON request in the post request I could pass a different value, like a username "bob" instead of 1.
so the idea is that instead of my request containing:
{
"client": 1,
"urgent": true,
"article": 1,
"quantity": 1,
"order_to": 1
}
it should contain:
{
"client": "bob",
"urgent": true,
"article": 234,
"quantity": 1,
"order_to": 1
}
here are my relevant models:
class UserModel(models.Model):
MEMBERSHIP_TYPE = [
('NM', 'NORMAL'),
('PT', 'PLATA'),
('OR', 'ORO'),
('PL', 'PLATINO'),
]
id = models.AutoField(primary_key=True)
username = models.CharField(max_length=100, unique=True)
photo = models.ImageField(upload_to='images/', blank=True)
address = models.TextField()
client_type = models.CharField(max_length=2,
choices=MEMBERSHIP_TYPE,
default= 'NM')
def __unicode__(self):
return self.name
class ArticleModel(models.Model):
id = models.AutoField(primary_key=True)
code = models.IntegerField(unique=True)
description = models.TextField()
def __str__(self):
return str(self.code)
class SupplierModel(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
address = models.TextField()
articles = models.ManyToManyField(ArticleModel)
def __str__(self):
return self.name
class OrderModel(models.Model):
id = models.AutoField(primary_key=True)
client = models.ForeignKey('UserModel', on_delete=models.CASCADE)
gen_time = models.DateTimeField(auto_now_add=True)
gen_supplied = models.DateTimeField(null=True, blank=True)
urgent = models.BooleanField()
order_to = models.ForeignKey('OrderToModel', on_delete=models.CASCADE)
article = models.ForeignKey('ArticleModel', on_delete=models.CASCADE)
quantity= models.IntegerField()
and Serializer:
class OrderCreateSerializer(serializers.ModelSerializer):
# order_to = OrderToPolymorphicSerializer()
class Meta:
model = OrderModel
fields = ('client', 'urgent', 'article', 'quantity', 'order_to')
Help is much appreciated.
Thank you in advance.
you can do this
class OrderCreateSerializer(serializers.ModelSerializer):
client = serializers.SlugRelatedField(
slug_field='username',
queryset=UserModel.objects.all()
)
article = serializers.SlugRelatedField(
slug_field='code',
queryset=ArticleModel.objects.all()
)
class Meta:
model = OrderModel
fields = ('client', 'urgent', 'article', 'quantity', 'order_to')

RelatedObjectDoesNotExist at /updateprofile User has no profile

i have one model userprofile which is connected to User by onetoone relationship. i am trying to create combined form for both user and userprofile form but it is showing onetoonedescripter has no attribute _meta.
views.py
'''
def UpdateProfile(request):
if request.method =='POST':
EUF_form = EditUserForm(request.POST, instance=request.user )
EPF_form = EditProfileForm(request.POST, instance=request.user.Profile )
if EUF_form.is_valid() and EPF_form.i_valid():
EUF_form.save()
EPF_form.save()
return HttpResponse("Profile updated succesfully.")
return render(request, 'profile_page.html')
else:
EUF_form = EditUserForm(instance=request.user)
EPF_form = EditProfileForm(instance=request.user.profile)
return render(request, 'edit_profile.html', {'EUF_form':EUF_form, 'EPF_form':EPF_form})
'''
models.py
'''
class Profile(models.Model):
user = models.OneToOneField(User, on_delete= models.CASCADE)
mobile = models.CharField(max_length=20, blank=True)
bio = models.CharField(max_length=300, default='')
avatar = models.ImageField(upload_to='avatars', blank=True)
birthday = models.DateField(blank=True, default='1990-12-01')
website = models.CharField(blank=True, max_length=256)
gender = models.CharField(blank=True, max_length=20, choices=[('M','Male'), ('F','Female'),('O','Other')])
def __str__(self):
return self.user.username
'''
forms.py
'''
class EditUserForm(ModelForm):
first_name = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control','type':'text','name': 'first_name'}),
label="First Name")
last_name = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control','type':'text','name': 'last_name'}),
label="Last Name")
username = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control','type':'text','name': 'username'}),
label="Username")
email = forms.EmailField(widget=forms.TextInput(
attrs={'class': 'form-control','type':'text','name': 'email'}),
label="Email")
class Meta:
model = User
fields = ['first_name','last_name','username','email']
class EditProfileForm(ModelForm):
mobile = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control','type':'tel','name': 'mobile'}),
label="Phone number")
bio = forms.CharField(widget=forms.Textarea,
label="Bio")
birthday = forms.DateField(widget=forms.DateInput(
attrs={'class': 'form-control','type':'date','name': 'birthday'}),
label="Username")
website = forms.URLField(widget=forms.URLInput(
attrs={'class': 'form-control','type':'url','name': 'website'}),
label="website")
gender = forms.ChoiceField(choices=[('M','Male'), ('F','Female'),('O','Other')],
widget=forms.Select ,
label="gender")
class Meta:
model = Profile
fields = ['mobile','bio','birthday','website','gender']
'''

KeyError Post DjangoRestFramework

I'm new on Django Rest Framework and when I want to POST data I get a error: KeyError: 'id_area' I do not know what I'm doing wrong. Here's my code:
in my models.py
class Area(models.Model):
id_area = models.AutoField(primary_key=True)
APM = 'apm'
BUSINESS = 'business'
DESARROLLO = 'desarrollo'
SISTEMAS = 'sistemas'
ATENTUSIANOS_CHOICES = (
(APM, 'Apm'),
(BUSINESS, 'Business'),
(DESARROLLO, 'Desarrollo'),
(SISTEMAS, 'Sistemas'),
)
nombre = models.CharField(max_length=255, choices=ATENTUSIANOS_CHOICES)
class Meta:
verbose_name = 'Área'
verbose_name_plural = 'Áreas'
def __str__(self):
return self.nombre
class Atentusiano(models.Model):
id_atentusiano = models.AutoField(primary_key=True)
nombre = models.CharField(max_length=255, blank=False, null=False)
apellido = models.CharField(max_length=255, blank=False, null=False)
correo = models.CharField(max_length=255, blank=False, null=False, unique=True)
anexo = models.CharField(max_length=255, blank=True, null=True)
area = models.ForeignKey(Area, related_name='areas', on_delete=models.CASCADE)
class Meta:
verbose_name = 'Atentusiano'
verbose_name_plural = 'Atentusianos'
ordering = ['nombre']
def __str__(self):
return self.nombre + ' ' + self.apellido
in my serializers.py
class AreaSerializer(serializers.ModelSerializer):
areas = serializers.CharField(read_only=True)
class Meta:
model = Area
fields = ('id_area', 'nombre', 'areas')
class AtentusianoSerializer(serializers.ModelSerializer):
atentusianos = serializers.CharField(read_only=True)
area = serializers.CharField(source='area.nombre', read_only=True)
id_area = serializers.CharField(source='area.id_area')
class Meta:
model = Atentusiano
fields = ['id_atentusiano', 'nombre', 'apellido', 'correo', 'anexo', 'id_area', 'area', 'atentusianos']
def create(self, validated_data):
area_data = validated_data.pop('id_area')
area = models.Area.objects.create(**area_data)
atentusiano = models.Atentusiano.objects.create(area=area, **validated_data)
return atentusiano
And in my views.py
class AtentusianoView(viewsets.ModelViewSet):
queryset = Atentusiano.objects.all()
serializer_class = AtentusianoSerializer
class AreaView(viewsets.ModelViewSet):
queryset = Area.objects.all()
serializer_class = AreaSerializer
The problem is that when I want to Post data, for example:
{
"nombre": "name",
"apellido": "lastname",
"correo": "email#gmail.com",
"anexo": "1364",
"id_area": "1"
}
i got this error area_data = validated_data.pop('id_area')
KeyError: 'id_area'
I need help please
you should pop like this,
class AtentusianoSerializer(serializers.ModelSerializer):
.....
.....
class Meta:
model = Atentusiano
fields = ['id_atentusiano', 'nombre', 'apellido', 'correo', 'anexo', 'id_area', 'area', 'atentusianos']
def create(self, validated_data):
id_area = validated_data.pop('area')['id_area'] # here the correction
area = Area.objects.create(id_area=id_area) # an additional correction
atentusiano = Atentusiano.objects.create(area=area, **validated_data)
return atentusiano
EDIT: As id_area value, you are passing a string instead of an integer which will through another error. Also not,
area = models.Area.objects.create(**area_data)
it should be,
area = Area.objects.create(id_area=id_area)

Creating ModelForm with EmbeddedModelField and customize fields within EmbeddedModelField

I have a model (parentmodel) which is having a EmbeddedModelField (embedmodel). This is basically a document in MongoDB. Below is the Model classes
class embedmodel(models.Model):
sendto = models.CharField(max_length=10)
sendtouser = models.CharField(max_length=15)
sendtogroup = models.CharField(max_length=15)
class parentmodel(models.Model):
name = models.CharField(max_length=30, unique=True, primary_key=True)
type = models.CharField(max_length=11)
enabled = models.BooleanField()
rule = models.EmbeddedModelField(model_container=embedmodel)
class Meta:
managed = False
db_table = 'parentmodel'
And this is how my document in mongodb looks like
{
'name': 'rule1',
'type': 'static',
'enabled': True,
'rule': {
'sendto': 'external',
'sendtouser': 'sam',
'sendtogroup': 'vendor'
}
}
I am trying to create a form which helps me create new rules and this is what i have in forms.py where i want to customize the form fields as well.
class RulesForm(forms.ModelForm):
name = forms.CharField(max_length=30, required=True)
type = forms.CharField(max_length=11, required=True)
enabled = forms.BooleanField(widget=forms.CheckboxInput)
class Meta:
model = parentmodel
fields = ['name', 'type', 'enabled', 'rule']
How to do customize the fields being displayed from embedmodel? I tried the below but no luck.
class RulesForm(forms.ModelForm):
name = forms.CharField(max_length=30, required=True)
type = forms.CharField(max_length=11, empty_value="UserDefined", required=True
enabled = forms.BooleanField(widget=forms.CheckboxInput)
sendto = forms.ChoiceField(widget=forms.Select, choices=[(1, 'External'), (2, 'Internal')])
sendtouser = forms.CharField(max_length=30, required=False)
sendtogroup = forms.CharField(max_length=30, required=False)
class Meta:
model = Rules
fields = ['name', 'type', 'enabled', 'rule']
and
class RulesForm(forms.ModelForm):
name = forms.CharField(max_length=30, required=True)
type = forms.CharField(max_length=11, empty_value="UserDefined", required=True
enabled = forms.BooleanField(widget=forms.CheckboxInput)
embedmodel.sendto = forms.ChoiceField(widget=forms.Select, choices=[(1, 'External'), (2, 'Internal')])
embedmodel.sendtouser = forms.CharField(max_length=30, required=False)
embedmodel.sendtogroup = forms.CharField(max_length=30, required=False)
class Meta:
model = Rules
fields = ['name', 'type', 'enabled', 'rule']
I was able to solve this by following https://medium.com/#SiddyZen/create-embedded-models-using-django-admin-3ecc38a00879 (in Section with heading The ‘embedded model’)Thanks to the author SiddyZen :). Did the below change in models.py
from django import forms
class embedmodel(models.Model):
sendto = models.CharField(max_length=10)
sendtouser = models.CharField(max_length=15)
sendtogroup = models.CharField(max_length=15)
class embedmodelForm(forms.ModelForm):
sendto = forms.ChoiceField(widget=forms.Select, choices=[(1, 'External'), (2, 'Internal')])
sendtouser = forms.CharField(max_length=15, required=False)
sendtogroup = forms.CharField(max_length=15, required=False)
class Meta:
model = embedmodel
fields = ['sendto', 'sendtouser', 'sendtogroup']
class parentmodel(models.Model):
name = models.CharField(max_length=30, unique=True, primary_key=True)
type = models.CharField(max_length=11)
enabled = models.BooleanField()
rule = models.EmbeddedModelField(model_container=embedmodel, model_form_class=embedmodelForm)
class Meta:
managed = False
db_table = 'parentmodel'

all forms are not saving to my database using class based views

I have an orderform that is not saving the items ordered to the database. I am only getting the contact form saved. I have searched and can't find what I am doing wrong or missing in my views. Any help would be greatly appreciated. Thank you!
Views:
class OrderFormView(CreateView):
model = Contact
form_class = ContactForm
template_name = 'orderform.html'
success_url = 'thank-you'
def get_context_data(self, **kwargs):
context = super(OrderFormView, self).get_context_data(**kwargs)
formsets = dict(
letterhead_formset=LetterHeadFormSet(prefix='letterhead'),
windowenv_formset=WindowEnvFormSet(prefix='windowenv'),
numbertenenv_formset=NumberTenEnvFormSet(prefix='numbertenenv'),
ninebytwelveenv_formset=NineByTwelveEnvFormSet(prefix='ninebytwelveenv'),
tenbythirteenenv_formset=TenByThirteenEnvFormSet(prefix='tenbythirteenenv'),
businesscard_formset=BusinessCardFormSet(prefix='businesscard'),
)
context.update({
'letterhead_form': LetterHeadForm,
'windowenv_form': WindowEnvForm,
'numbertenenv_form': NumberTenEnvForm,
'ninebytwelveenv_form': NineByTwelveEnvForm,
'tenbythirteenenv_form': TenByThirteenEnvForm,
'businesscard_form': BusinessCardForm,
'rushorder_form': RushOrderForm,
'ordernote_form': OrderNoteForm,
})
context.update(formsets.items())
return context
def form_valid(self, form):
if form.is_valid():
data = form.cleaned_data
email = OrderFormNotification(to=[settings.NO_REPLY_EMAIL_ADDRESS, ],
extra_context=data)
email.send()
form.save()
return super(OrderFormView, self).form_valid(form)
Form Classes:
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)
Models:
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()
form_valid and the code you've added to it, only processes the actual form specified by form_class. You have to manually validate and save each formset.