update: found error...
class basecontactpersonformset(forms.models.basemodelformset): def __init__(self, company, data=none, **kwargs): super(basecontactpersonformset, self).__init__(**kwargs) form in self: form.company = company contactpersonformset = forms.models.modelformset_factory(model=person, form=singlecontactpersonform, formset=basecontactpersonformset, can_delete=true, extra=0)
the piece above caused form never save, dunno why. removing , doing relatively hacky workaround fixed me. have modelselect named company
in form filled in using javascript.
original question:
i have django view displays , saves formset, code of is:
def manage_company_contact_persons(request, company_id): company = get_object_or_404(company, id=company_id) if request.method == 'post': print request.post cpfs = forms.contactpersonformset(queryset=company.contact_persons.all(), data=request.post) if cpfs.is_valid(): cpf in cpfs: print cpf.errors cpf.save() else: cpfs = forms.contactpersonformset(queryset=company.contact_persons.all()) return render_to_response('company/manage/admin-crm-company-contactpersons-new.html', {'contactpersons': cpfs, 'company': company,}, context_instance=requestcontext(request))
the purpose of allow user create/update/delete contractpersons database. contactpersonformset defined follows:
class singlecontactpersonform(forms.modelform): # fields user model first_name = forms.charfield(widget=forms.textinput(attrs={'class': 'w170'}), required=true, label=_(u'first name')) last_name = forms.charfield(widget=forms.textinput(attrs={'class': 'w170'}), required=true, label=_(u'last name')) email = forms.emailfield(widget=forms.textinput(attrs={'class': 'w170'}), required=true, label=_(u'e-mail')) password = forms.charfield(widget=forms.passwordinput(attrs={'class': 'w170'}), required=false, label=_(u'password')) # fields address model zip_code = forms.charfield(widget=forms.textinput(attrs={'class': 'w100 address'}), required=false, label=_(u'zip code + house number')) house_number = forms.charfield(widget=forms.textinput(attrs={'class': 'w60 address'}), required=false, label=_(u'zip code + house number')) street = forms.charfield(widget=forms.textinput(attrs={'class': 'w170 address'}), required=false, label=_(u'street')) city = forms.charfield(widget=forms.textinput(attrs={'class': 'w170 address'}), required=false, label=_(u'city')) country = forms.modelchoicefield(queryset=country.objects.all(), widget=forms.select(attrs={'class address': 'w170'}), required=false, label=_(u'country')) def __init__(self, *args, **kwargs): super(singlecontactpersonform, self).__init__(*args, **kwargs) ### if there instance, fill fields def save(self, commit=true, *args, **kwargs): '''saves whole contact person database, including address , user.''' super(singlecontactpersonform, self).save(*args, commit=commit, **kwargs) ### save data in other models if commit: super(singlecontactpersonform, self).save() return self class meta: model = person fields = ['job_title', 'telephone_number', 'mobile_number', 'fax'] widgets = {'job_title': forms.textinput(attrs={'class': 'w170'}), 'telephone_number': forms.textinput(attrs={'class': 'w170'}), 'mobile_number': forms.textinput(attrs={'class': 'w170'}), 'fax': forms.textinput(attrs={'class': 'w170'}), } contactpersonformset = forms.models.modelformset_factory(model=person, form=singlecontactpersonform, can_delete=true, extra=0)
the issue reason original contact person saved database. new contact persons never created, save() method new ones never called. data in post far can see. formset valid, because original items save() function called.
is maybe falling on fact include empty 'id' in post data?
the data in post is:
{u'csrfmiddlewaretoken': [u'056d5042e73322c8166011e27108d362'], u'form-0-city': [u'culemborg'], u'form-0-country': [u'155'], u'form-0-email': [u'info@domain1.com'], u'form-0-fax': [u'0345750249'], u'form-0-first_name': [first'], u'form-0-house_number': [u'7'], u'form-0-id': [u'69'], u'form-0-job_title': [u'job'], u'form-0-last_name': [u'name'], u'form-0-mobile_number': [u''], u'form-0-password': [u''], u'form-0-street': [u'street'], u'form-0-telephone_number': [u'0123456789'], u'form-0-zip_code': [u'4104an'], u'form-1-city': [u'culemborg'], u'form-1-country': [u'155'], u'form-1-email': [u'info@domain2.com'], u'form-1-fax': [u''], u'form-1-first_name': [u'second'], u'form-1-house_number': [u'7'], u'form-1-id': [u''], u'form-1-job_title': [u'function'], u'form-1-last_name': [u'name'], u'form-1-mobile_number': [u''], u'form-1-password': [u''], u'form-1-street': [u'nijverheidsweg'], u'form-1-telephone_number': [u'0987654321'], u'form-1-zip_code': [u'4104an'], u'form-initial_forms': [u'1'], u'form-max_num_forms': [u'1000'], u'form-total_forms': [u'1'], u'form-__prefix__-city': [u''], u'form-__prefix__-country': [u''], u'form-__prefix__-email': [u''], u'form-__prefix__-fax': [u''], u'form-__prefix__-first_name': [u''], u'form-__prefix__-house_number': [u''], u'form-__prefix__-id': [u''], u'form-__prefix__-job_title': [u''], u'form-__prefix__-last_name': [u''], u'form-__prefix__-mobile_number': [u''], u'form-__prefix__-password': [u''], u'form-__prefix__-street': [u''], u'form-__prefix__-telephone_number': [u''], u'form-__prefix__-zip_code': [u'']}
edit:
i think found error. forgot pass "data" arg "super" call. wrote this:
class basecontactpersonformset(forms.models.basemodelformset): def __init__(self, company, data=none, **kwargs): super(basecontactpersonformset, self).__init__(**kwargs) form in self: form.company = company contactpersonformset = forms.models.modelformset_factory(model=person, form=singlecontactpersonform, formset=basecontactpersonformset, can_delete=true, extra=0)
but should write this:
class basecontactpersonformset(forms.models.basemodelformset): def __init__(self, company, data=none, **kwargs): super(basecontactpersonformset, self).__init__(data, **kwargs) form in self: form.company = company contactpersonformset = forms.models.modelformset_factory(model=person, form=singlecontactpersonform, formset=basecontactpersonformset, can_delete=true, extra=0)
old answer:
try changing these lines:
if cpfs.is_valid(): cpf in cpfs: print cpf.errors cpf.save()
for these lines:
if cpfs.is_valid(): cpfs.save()
taken here
Comments
Post a Comment