Maybe I can help Luc? I had similar questions once, because I started
learning Django while deploying with a legacy database.

On 8 fev, 20:31, Luc Saffre <luc.saf...@gmx.net> wrote:
> You cannot ask user code to not even look at invalid data. I'm
> not allergic to exceptions, but raising an exception when I ask for the
> content of a field is not appropriate behaviour.
>
> Luc

Raising the exception *is* appropriate behaviour, because when you
access `a_model.b_related`, and `b_related` is a ForeignKey, you're
not really accessing a value, but doing a lookup. This is consistent
with both the ER and ORM model, where FK's are pointers, not values.
And a FK pointing to a non-existant row *is* an exception. It means
broken data.

If you want the content of the *field*, what you really should check
is `a_model.b_related_id is None`, which is checking if a pointer was
set. Still, it doesn't guarantee that this pointer leads to anywhere
valid. That's why dealing with the exception is a common idiom, "fail
early, fail loudly", and particularly useful in this case to maintain
DB consistency.

I hope it helps in understanding the rationale behind the behaviour
and why Luke marked as invalid.

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