karanlyons / django-save-the-change

Your DB Got It the First Time.
http://django-save-the-change.readthedocs.io
Other
114 stars 29 forks source link

Creating new instances causes DoesNotExist error #2

Closed hellsgate1001 closed 11 years ago

hellsgate1001 commented 11 years ago

While Updating an existing model instance works as expected, creating a new instance causes a DoesNotExist exception.:

Request Method: POST Request URL: http://corecrm.local/booking/dining/

Django Version: 1.5.3 Python Version: 2.7.3 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.gis', 'nested_inlines', 'django.contrib.admin', 'south', 'rest_framework', 'djcelery', 'django_extensions', 'corecrm', 'people', 'groupop', 'venue', 'tag', 'booking', 'debug_toolbar') Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'audit_log.middleware.UserLoggingMiddleware', 'debug_toolbar.middleware.DebugToolbarMiddleware')

Traceback: File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response

  1. response = callback(request, _callback_args, *_callback_kwargs) File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
  2. return self.dispatch(request, _args, *_kwargs) File "C:\sites\corecrm\corecrm\mixins.py" in dispatch
  3. **kwargs) File "C:\Python27\lib\site-packages\braces\views.py" in dispatch
  4. **kwargs) File "C:\sites\corecrm\booking\views.py" in dispatch
  5. return super(BaseBookingCreateView, self).dispatch(request, _args, *_kwargs) File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
  6. return handler(request, _args, *_kwargs) File "C:\Python27\lib\site-packages\django\views\generic\edit.py" in post
  7. return super(BaseCreateView, self).post(request, _args, *_kwargs) File "C:\Python27\lib\site-packages\django\views\generic\edit.py" in post
  8. if form.is_valid(): File "C:\Python27\lib\site-packages\django\forms\forms.py" in is_valid
  9. return self.is_bound and not bool(self.errors) File "C:\Python27\lib\site-packages\django\forms\forms.py" in _get_errors
  10. self.full_clean() File "C:\Python27\lib\site-packages\django\forms\forms.py" in full_clean
  11. self._post_clean() File "C:\Python27\lib\site-packages\django\forms\models.py" in _post_clean
  12. self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude) File "C:\Python27\lib\site-packages\django\forms\models.py" in construct_instance
  13. f.save_form_data(instance, cleaned_data[f.name]) File "C:\Python27\lib\site-packages\django\db\models\fieldsinit.py" in save_form_data
  14. setattr(instance, self.name, data) File "C:\Python27\lib\site-packages\save_the_change\mixins.py" in setattr
  15. old = getattr(self, name, DoesNotExist) File "C:\Python27\lib\site-packages\django\db\models\fields\related.py" in get
  16. raise self.field.rel.to.DoesNotExist

Exception Type: DoesNotExist at /booking/dining/ Exception Value:

hellsgate1001 commented 11 years ago

I'm looking at this just now and I'll issue a pull request if I manage to fix this.

hellsgate1001 commented 11 years ago

Sorry, closed by mistake :(

hellsgate1001 commented 11 years ago

Issue fixed and pull request issued

karanlyons commented 11 years ago

Ah, I think I understand what's going on here, though strangely I haven't tripped it myself. Adding a self.pk check should solve the problem, but it may not be the best solution, given that it prevents STC from doing anything potentially helpful for you when a new instance is created (less a problem for the main mixin, but for people using TrackChanges, it might suck a bit).

Could you add a test that breaks the current build of STC?

karanlyons commented 11 years ago

Fixed by #4.