On 12.02.2010 14:01, Tom Evans wrote:
> I think the easiest way of understanding the behaviour is that
> specifying a field like this:
> 
>    owner = models.ForeignKey(Owner)
> 
> specifies a contract. The contract says that when you access this
> attribute on an instance, it will only return an instance of an Owner
> object or raise an DoesNotExist exception.
> 
> Specifying a field like this:
> 
>   owner = models.ForeignKey(Owner,blank=True,null=True)
> 
> specifies a very different contract. This contract says that accessing
> this attribute will return either an instance of an Owner object, or
> None if no owner has been specified.
> 
> Both contracts are fully described in the documentation and fulfilled
> by the ORM, so clearly this is no bug. Both flavours are available for
> you to use, you simply have to decide what sort of contract you want
> that attribute to have - if you expect instance.owner to return None,
> then clearly you should be using a nullable foreign key.

Wow! This formulation is really clear. Thank you, Tom.

My summary: Django's behaviour is un-pythonic and not the most elegant
one, but it is possible to get used to it, even to rely on it, and it
certainly won't change.

I didn't find such a good explanation in the docs, though. IMHO it
should be here:

http://docs.djangoproject.com/en/dev/topics/db/queries/#forward

I suggest to add a paragraph about the surprising DoesNotExist there.

Luc

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to