I have a Question and an Answer table with a relationship between
them, defined as follows in models.py:

class Question(models.Model):
    queid = models.IntegerField(primary_key=True, db_column="queid")
    quedomid = models.ForeignKey(Domain, db_column="quedomid")
    quedqno = models.IntegerField("Question #")
    quetext = models.TextField("Question Text")
    def __str__(self):
        return "%s-%s" % (self.quedomid, self.quedqno)
    class Meta:
        db_table = "question"
        ordering = ['quedomid', 'quedqno']
    class Admin:
        pass

class Answer(models.Model):
    ansid = models.IntegerField(primary_key=True, db_column="ansid")
    ansqueid = models.ForeignKey(Question, db_column="ansqueid")
    anstext = models.TextField("Answer Text")
    ansresult = models.BooleanField("Correct?")
    ansseq = models.CharField("Sequence", max_length=1)
    def __str__(self):
        q = Question.get(self.ansqueid)
        return "%s:%s" % (str(q), self.ansseq)
    class Meta:
        db_table = "answer"
        ordering = ['ansseq']
    class Admin:
        pass

I am using a ModelForm to allow update of the answers:

class AnswerForm(forms.ModelForm):
    ansid = forms.IntegerField(widget=forms.HiddenInput())
    ansqueid = forms.IntegerField(widget=forms.HiddenInput())
    class Meta:
        model = Answer

as you can see, I hide the primary and foreign key fields (yes, I am
aware of the security implications: can we fix the real problem
please ;-)

The method used to process this form is as follows:

def answer(request, domid, quedqno, ansseq, ansid):
    question_no = "%s-%s" % (domid, quedqno)
    if request.method == 'POST':
        form = AnswerForm(request.POST)
        if form.is_valid():
            answer = form.save()
            return HttpResponseRedirect("/edit/question/%s/%s/" %
(domid, quedqno))
    else:
        domain = Domain.objects.get(pk=domid)
        question = Question.objects.get(quedomid=domain.domid,
quedqno=quedqno)
        answer = Answer.objects.get(ansqueid=question.queid,
ansseq=ansseq)
        form = AnswerForm(instance=answer)
    return render_to_response("answer.html", locals(),
RequestContext(request))

When the method is called with a GET I see what appears to be a
correctly-filled form in the HTML, including

<input type="hidden" name="ansid" value="1345" id="id_ansid" />
<input type="hidden" name="ansqueid" value="341" id="id_ansqueid" />

When I submit the form I see:

IntegrityError at /edit/answer/0/7/a/1345/
null value in column "ansqueid" violates not-null constraint

Sure enough the SQL that django is trying to execute is

'UPDATE "answer" SET "ansqueid"=NULL,"anstext"=\'With hashing, the
algorithm must be known to both sender and receiver
\',"ansresult"=false,"ansseq"=\'a\' WHERE "ansid"=1345'

Can anyone tell me why ansqueid is being set to NULL? Traceback, if
significant, follows.

Environment:

Request Method: POST
Request URL: http://localhost:8000/edit/answer/0/7/a/1345/
Django Version: 0.97-pre-SVN-7054
Python Version: 2.5.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'quiz']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File "C:\Steve\django-trunk\django\core\handlers\base.py" in
get_response
  82.                 response = callback(request, *callback_args,
**callback_kwargs)
File "C:\Steve\quiz446\..\quiz446\edit\views.py" in answer
  46.             answer = form.save()
File "C:\Steve\django-trunk\django\newforms\models.py" in save
  289.         return save_instance(self, self.instance,
self._meta.fields, fail_message, commit)
File "C:\Steve\django-trunk\django\newforms\models.py" in
save_instance
  56.         instance.save()
File "C:\Steve\django-trunk\django\db\models\base.py" in save
  239.                         db_values +
self._meta.pk.get_db_prep_lookup('exact', pk_val))
File "C:\Steve\django-trunk\django\db\backends\util.py" in execute
  18.             return self.cursor.execute(sql, params)

Exception Type: IntegrityError at /edit/answer/0/7/a/1345/
Exception Value: null value in column "ansqueid" violates not-null
constraint

head-scratching-ly y'rs  - steve
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to