#24418: refresh_from_db is breaking on null foreign keys
-------------------------------------+-------------------------------------
     Reporter:  ppp23                |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.8alpha1
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:  fixed
     Keywords:                       |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by ppp23:

Old description:

> If i try to call refresh_from_db on a model instance that has a
> foreignkey set to null, i get an error:
>
> {{{
> Traceback:
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/core/handlers/base.py" in get_response
>   112.                     response = wrapped_callback(request,
> *callback_args, **callback_kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/contrib/admin/options.py" in wrapper
>   465.                 return self.admin_site.admin_view(view)(*args,
> **kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/utils/decorators.py" in _wrapped_view
>   99.                     response = view_func(request, *args, **kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/views/decorators/cache.py" in _wrapped_view_func
>   52.         response = view_func(request, *args, **kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/contrib/admin/sites.py" in inner
>   198.             return view(request, *args, **kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/utils/decorators.py" in _wrapper
>   29.             return bound_func(*args, **kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/utils/decorators.py" in _wrapped_view
>   99.                     response = view_func(request, *args, **kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/utils/decorators.py" in bound_func
>   25.                 return func(self, *args2, **kwargs2)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/db/transaction.py" in inner
>   371.                 return func(*args, **kwargs)
> File "/home/johannes/formonauts/donation/.env/local/lib/python2.7/site-
> packages/django/contrib/admin/options.py" in add_view
>   1164.                 self.save_model(request, new_object, form, False)
> File "/home/johannes/formonauts/donation/server/django-
> donut/donut/admin.py" in save_model
>   36.                 obj.save()
> File "/home/johannes/formonauts/donation/server/django-
> donut/nanny/models.py" in save
>   109.                 self.refresh_from_db()
> File "/home/johannes/formonauts/donation/server/django-
> donut/donut/basemodels.py" in refresh_from_db
>   80.                 related_val = getattr(rel_instance,
> field.related_field.attname)
>
> Exception Type: AttributeError at /admin/nanny/team/add/
> Exception Value: 'NoneType' object has no attribute 'id'
> }}}
>
> here rel_instance is assigned:
> https://github.com/django/django/blob/stable/1.8.x/django/db/models/base.py#L630
>
> if it is None (foreignkey not set, as it is nullable in this model) the
> getattr function will complain that it can not get 'id' from 'NoneType'

New description:

 If i try to call refresh_from_db on a model instance that has a foreignkey
 set to null, i get an error:

 (stacktrace was removed after ticket was resolved)

 here rel_instance is assigned:
 
https://github.com/django/django/blob/stable/1.8.x/django/db/models/base.py#L630

 if it is None (foreignkey not set, as it is nullable in this model) the
 getattr function will complain that it can not get 'id' from 'NoneType'

--

--
Ticket URL: <https://code.djangoproject.com/ticket/24418#comment:6>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.bd6f7c30717f2824849821e4c4bbdf39%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to