Wednesday, November 2, 2011

Formwizard - Many2Many field - instance needs to have a primary key value before a many-to-many relationship can be used

Hello,
i'm currently doning a website where user can register (without
password, without auth module of Django). They put some informations
and at the end (Step5Form) do multiple choices by clicking on
checkboxes. When I click on validate on the last step I've "instance
needs to have a primary key value before a many-to-many relationship
can be used" error. I've search the web to find a solution but ..... :-
(

Here is my code:

models.py for Customer (in clients application)
---------------------------------------------
class Customer (models.Model):

email_adresse = models.EmailField(max_length=255, unique=True)
some fields ..... for Step2 to Step4 of the form
category = models.ManyToManyField(Categories)
---------------------------------------------

models.py for categories (in categories application)
---------------------------------------------
class Categories (models.Model):
category = models.CharField(max_length=200)

def __unicode__(self):
return unicode(self.category)
---------------------------------------------

The ManyToMany field has created(syncdb) a table
clients_customer_category with the following columns:
1 id int(11) AUTO_INCREMENT
2 customer_id int(11)
3 categories_id int(11)


urls.py
---------------------------------------------
urlpatterns = patterns('',
(r'^clients/$', InscriptionWizard.as_view([Step1Form, Step2Form,
Step3Form, Step4Form, Step5Form])),
)
---------------------------------------------


forms.py (in clients application)
---------------------------------------------
from django import forms
from clients.models import Customer
from categories.models import Categories

class Step1Form(forms.Form):
email_adresse = forms.EmailField(max_length=255)

class Step5Form(forms.Form):
category =
forms.ModelMultipleChoiceField(queryset=Categories.objects.all(),
widget=forms.CheckboxSelectMultiple, required=True)


views.py (in clients application)
---------------------------------------------
class InscriptionWizard(SessionWizardView):
def done(self, form_list, **kwargs):
instance = Customer()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
setattr(instance, field, value)
instance.save()
---------------------------------------------

I got the following error:
---------------------------------------------
Request Method: POST
Request URL: http://127.0.0.1:8010/clients/
Django Version: 1.3.1
Exception Type: ValueError
Exception Value:
'Customer' instance needs to have a primary key value before a many-to-
many relationship can be used.
...............
...............
▶ Local vars
C:\dev\Flash\clients\views.py in done
setattr(instance, field, value) ...
---------------------------------------------


What I've tried:
1)I've search the web and didn't find any way to solve this. I've read
on save_m2m() wich doesn't apply here as far I understand because no
"commit=False" and this form is not a ModelForm.


2)I've trie to save in 2 steps with the hope an id has been set in the
DB:
-First step : for form in form_list[0:3]: ...... instance.save()
-Second step :
for form in form_list[4]:
for field, value in form.cleaned_data.iteritems():
instance.category.add(value)

I've the following error : 'BoundField' object has no attribute
'cleaned_data' for the line "for field, value in
form.cleaned_data.iteritems():" in Second step.


Then I can maybe split my form in 2 forms, one from Step0 to Step3 and
another one for Step4 and passing the id_customer in teh session but
maybe there is a solution to solve this issue in one form.


Help will be really appreciated.

I apologize if my explanations are not enough clear (and for my
english) and feel free to ask informations.

Regards

Alain


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment