#18416: Query fails when SimpleLazyObject evaluates to None
-------------------------------------+-------------------------------------
     Reporter:  bouke                |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.4
  (models, ORM)                      |               Resolution:  wontfix
     Severity:  Normal               |             Triage Stage:
     Keywords:                       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by dougvanhorn):

 I just bumped into this bug.  I'm not going to muck with the status, but I
 wanted to document my thoughts in case anyone else wants to take up the
 cause on this (I've found it to be a tedious, political process).

 The code that raises the exception is this (it's found in most of the
 Field types):

 {{{
 #!python
 def get_prep_value(self, value):
     if value is None:
         return None
     return int(value)
 }}}

 It's the `value is None` that causes the problem.
 [http://stackoverflow.com/questions/15399024/does-the-is-operator-use-a
 -magic-method-in-python It's an identity test].

 If the intent of the if-test is return `None` when the supplied value
 equates to `None`, then the if-test should be re-written as `value ==
 None`, which would test for equality.  The equality test is slower,
 though, so you'll likely find argument against using it in this section of
 the code.  Oddly built objects with __eq__ overloaded might also find
 trouble.

 So there may not be much to do here, leaving the burden on the user to
 figure out what's going on.  But I wanted to add some documentation to
 this bug for future reference.


 ----


 For what it's worth, I ran into this problem with the following code:

 {{{
 #!python
 try:
     agreement = Agreement.objects.get(user=request.user)
 except Agreement.DoesNotExist, e:
     agreement = Agreement(user=request.user)
 }}}

 I ended up wrapping the view in the `login_required` method, which makes
 the `.user` evaluate to something, preventing the bug.  But as stated by
 **bouke**, the stack is not very helpful.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18416#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to