Opened 10 years ago

Last modified 10 years ago

#24418 closed Bug

refresh_from_db is breaking on null foreign keys — at Initial Version

Reported by: Johannes Lerch Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8alpha1
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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'

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top