Excuse me for not writing back earlier - travelling isn't always easy. I have however, figured out the cause of this problem and fixed it. I believe it had to do with using ModelMultipleChoiceField instead of ModelChoiceField. Thanks for clearing that up. I will also make a note to be more precise with the snippets I put up in the future.
I still need some help with selecting multiple options, but since it doesn't exactly fit under this topic, I'll post a new message up. Thanks, R On Tue, Apr 22, 2008 at 9:41 PM, Karen Tracey <[EMAIL PROTECTED]> wrote: > > On Mon, Apr 21, 2008 at 12:06 PM, Rishabh Manocha <[EMAIL PROTECTED]> > wrote: > > > > > > Thanks for writing back, Karen. I guess the snippets of code I > > inserted were a little misleading. I have many other modelforms too, > > which all have a user = ForeignKey(User) defined. Problem is that I > > have to use multiple instances of these forms (for example, I need I > > have a ModelForm called PrevoiusOrganisation. Each instance represents > > one org, but I want the user to be able to enter multiple previous > > organisations ) which all have to have their "user" set. An example > > is: > > > > if request.POST: > > new_user = UserDetailsForm(request.POST, request.FILES, > instance=User()) > > prevorgforms = [PrevOrgsForm(request.POST, > > prefix="prevorgs"+str(x), instance = PrevOrgs()) for x in range(0, 3)] > > prevorgprojsforms = [PrevOrgProjsForm(request.POST, > > prefix="prevorgprojs"+str(x), instance = PrevOrgProjs()) for x in > > range(0, 4)] > > if new_user.is_valid() and all([prevorgform.is_valid() for > > prevorgform in prevorgforms]) and ...: > > new_user_id = new_user.save() > > set_user_id(prevorgforms,new_user_id) > > set_user_id(prevorgprojsforms,new_user_id) > > ... > > > > def set_user_id(the_list, the_user_id): > > for list_elem in the_list: > > new_list_elem = list_elem.save(commit=False) > > new_list_elem.user = the_user_id > > new_list_elem.save() > > > > I have not had any issues with this function and my other modelforms, > > so I don't think I'm doing something wrong here. I suspect something > > is missing either during the Ajax call or later when I set the > > queryset for each list on submission. > > > > Hope that clears up my problem a little bit more. > > > > Well no, that didn't really clear things up. You were asking about trouble > saving a SkillListForm and now are posting about how set_user_id is called > when you are doing things with a UserDetailsForm and some different > organization forms with multiple instances, which is confusing since it > seems entirely unrelated to the original problem. I gather what you are > trying to say, though, is that this same set_user_id function is called when > you are doing the processing to save a SkillListForm? If so I'm not sure > why you didn't just post the code involved when saving a SkillListForm. > > Anyway, on closer inspection of the SkillListForm I do notice a problem: you > override skill like so: > > > skill = forms.ModelMultipleChoiceField(required = True, label = "Technical > Skill",queryset = A.objects.none()) > > This should be a ModelChoiceField, not a ModelMultipleChoiceField. (Also > the queryset should be tied to the B model, not A, since skill is a > ForeignKey specifying a B model instance.) > > However, though this error would produce exactly the symptom you describe, > I'm not entirely sure that is the cause of your problem because you say your > form displays two drop-down boxes and a ModelMultipleChoiceField wouldn't > have a drop-down widget, so I am not sure the code you posted really matches > what you are running (there are also some typos in the ForeignKey > definitions, so it seems you re-typed vs. cut-and-pasted your code). > People are much more likely to be able to provide useful help when you post > exactly the code you are running. If you feel it is too complicated by all > means try to simplify it, but verify you still hit the problem with the > simplified code. Oftentimes in the process of simplifying you can identify > what is causing the problem. > > Karen > > > > > Best, > > > > R > > > > > > > > > > > > On Mon, Apr 21, 2008 at 6:38 PM, Karen Tracey <[EMAIL PROTECTED]> wrote: > > > > > > On Mon, Apr 21, 2008 at 2:53 AM, Rishabh Manocha <[EMAIL PROTECTED]> > wrote: > > > > > > > > > > > > > > Hey Guys, > > > > > > > > I have 4 models working something like this: > > > > > > > > class User(models.Model): > > > > name = models.CharField() > > > > email = models.EmailField() > > > > ... > > > > > > > > class A(models.Model): > > > > domainname = models.CharField() > > > > > > > > class B(models.Model): > > > > domain = models.ForigenKey(A) > > > > name = models.CharField() > > > > > > > > class C(models.Model): > > > > user = models.ForiegnKey(User) > > > > skill = models.ForeignKey(B) > > > > years = models.IntegerField() > > > > > > > > The reason class C exists is because A & B will be used as drop down > > > > lists whose values only the admin should enter through the admin > > > > interface. That bit is working ok. > > > > > > > > I use ModelForms to load the form for C (like this): > > > > > > > > class SkillListForm(forms.ModelForm): > > > > domainname = forms.ModelChoiceField(required=True, label = > > > > "Domain", widget = forms.Select({"onchange" : "javascript: > > > > updateTechSkillsList(this);"}),queryset = A.objects.all()) > > > > skill = forms.ModelMultipleChoiceField(required = True, label = > > > > "Technical Skill",queryset = A.objects.none()) > > > > > > > > class Meta: > > > > model = C > > > > exclude = ("user",) > > > > > > > > The form, as you can see, produces two drop down lists. The second one > > > > is blank to begin with, but the javascript function > > > > (updateTechSkillsList) populates it based on the selection in the > > > > first list. The JS function makes an Ajax call to a URL whose function > > > > in views.py is: > > > > > > > > def populate_techskills(request): > > > > if request.GET: > > > > domain = get_object_or_404(A, pk=request.GET['domain_id']) > > > > choices = domain.b_set.all() > > > > choices_html = "" > > > > for choice in choices: > > > > choices_html += "<option value=\"%s\">%s</option>" % > > > > (choice.id, choice.name) > > > > return HttpResponse(choices_html) > > > > else: > > > > return HttpResponseRedirect("/userform/") > > > > > > > > When the form is finally submitted, I reset the queryset for skills > > > > after checking for request.POST so that the values populated by JS and > > > > the values being held by the form instance are the same: > > > > myform.fields['skill'].queryset = A.objects.get(pk = > > > > request.POST["techskills-domainname"]).b_set.all() > > > > > > > > The form then validates ok (passes myform.is_valid()) however, when it > > > > comes time to save it, I keep getting the following error: > > > > > > > > Traceback: > > > > File "C:\Python25\Lib\site-packages\django\core\handlers\base.py" in > > > > get_response > > > > 82. response = callback(request, *callback_args, > > > > **callback_kwargs) > > > > File "C:\Documents and > > > > > > > > Settings\rmanocha\workspace\TechSkillsInventory\..\TechSkillsInventory\users\views.py" > > > > in index > > > > 57. set_user_id(techskillslistforms, new_user_id) > > > > File "C:\Documents and > > > > > > > > Settings\rmanocha\workspace\TechSkillsInventory\..\TechSkillsInventory\users\views.py" > > > > in set_user_id > > > > 12. new_list_elem.save() > > > > File "C:\Python25\Lib\site-packages\django\db\models\base.py" in save > > > > 264. ','.join(placeholders)), db_values) > > > > File "C:\Python25\Lib\site-packages\django\db\backends\util.py" in > execute > > > > 18. return self.cursor.execute(sql, params) > > > > File "C:\Python25\Lib\site-packages\MySQLdb\cursors.py" in execute > > > > 166. self.errorhandler(self, exc, value) > > > > File "C:\Python25\Lib\site-packages\MySQLdb\connections.py" in > > > > defaulterrorhandler > > > > 35. raise errorclass, errorvalue > > > > > > > > Exception Type: OperationalError at /userform/ > > > > Exception Value: (1048, "Column 'skill_id' cannot be null") > > > > > > > > > > > > The POST variable has a value for skill, and printing out > > > > form.cleaned_data['skill'] inside SkillListForm.clean_skill prints the > > > > correct id. However, I am still getting this error. Can anyone see > > > > what I'm doing wrong here?? > > > > > > > > > > From the traceback I don't see any evidence that the problem is > occurring > > > when you try to save the form you have posted details on. Rather your > > > views.py index function is calling a function named set_user_id, which > is > > > calling save on something named 'new_list_elem'. None of that sounds > like > > > processing for a posted form, so I'm suspecting the problem is coming > from > > > some other code you haven't mentioned and aren't looking at because you > are > > > focused on the form-processing code. It sounds more like perhaps the > form > > > save is working and redirects to 'index' but then the code that handles > > > index is running into trouble? > > > > > > As an aside, are you really using you own model named User? This just > seems > > > like it could lead to confusion with the Django-provided User model in > > > contrib.auth.models. > > > > > > Karen > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---