views.py (in clients application)
---------------------------------------------
class InscriptionWizard(SessionWizardView):
def done(self, form_list, **kwargs):
instance = Customer()
---------------------------------------------
class InscriptionWizard(SessionWizardView):
def done(self, form_list, **kwargs):
instance = Customer()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
if field == 'category':
instance.save()
setattr(instance, field, value)
This would of course depend on your ordering of the fields. In other words, category would have to be the first Allow Null field in your model, because you cannot save() until all Not Null fields have a value. If you cannot order these fields so the category is last, you may have to do something like:
setattr(instance, field, value)
instance.save()
---------------------------------------------This would of course depend on your ordering of the fields. In other words, category would have to be the first Allow Null field in your model, because you cannot save() until all Not Null fields have a value. If you cannot order these fields so the category is last, you may have to do something like:
views.py (in clients application)
---------------------------------------------
class InscriptionWizard(SessionWizardView):
def done(self, form_list, **kwargs):
instance = Customer()
---------------------------------------------
class InscriptionWizard(SessionWizardView):
def done(self, form_list, **kwargs):
instance = Customer()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
if field != 'category':
setattr(instance, field, value)
instance.save()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
if field == 'category':
setattr(instance, field, value)
---------------------------------------------I'm sure there is a MUCH more elegant way of getting just the categories from the forms and field/value pairs, this is brute force (not very scalable for large number of fields), but should work. Anybody else please, show code that could get the specific forms and field/values for categories.
Furbeenator
On Wed, Nov 2, 2011 at 10:28 AM, youpsla <youpsla@gmail.com> wrote:
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.
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