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 -~----------~----~----~----~------~----~------~--~---