On Sat, Jun 04, 2011 at 12:21:37PM -0700, Ryan wrote:
> Right, if what you have typed in your post is the code you are actually 
> trying to use, there are two problems that jump out at me.
> 
> The first is that *question = models.CaaQuestion(pk=210) *will actually 
> create a new question with the primary key 210, not fetch one from the 
> database, so when you try to use this to find answers in the subsequent 
> lines it will not find anything as it is not currently in any of those 
> tables.
I don't think this is entirely true. You're right in the fact that the
created CaaQuestion instance will not be fetched nor saved, that
however doesn't mean it isn't in the database.

However, it is correct to use such instance in filters. In that case,
django picks up the vlue of the target field from the instance and
uses that to build the database query. If there is no such row in the
table with this ID, the QuerySet will just return an empty result.

> The second if the FieldError you are receiving.  This is because the 
> question_id and assessment_id arguments should have double underscores to 
> tell django that you are trying to span a relationship.  So these two should 
> actually be question__id and assessment__id.  However the line above that 
> with question = question and assessment = assessment should work fine when 
> you have a question that is actually in the database.
This is correct under the assumption that Assessment has id as its
primary key. Otherwise you should use _pk to be on the safe side.

Nevertheless, when my patch for
https://code.djangoproject.com/ticket/5535 gets in, it will be
possible to specify the primary key value explicitly, the same way
Fred tried to.

Michal

Attachment: signature.asc
Description: Digital signature

Reply via email to