Django

Code

Ticket #11194 (closed: fixed)

Opened 10 months ago

Last modified 7 months ago

Saving proxy model with raw=True gives UnboundLocalError 'record_exists'

Reported by: wardi Assigned to: nobody
Milestone: 1.1 Component: Database layer (models, ORM)
Version: 1.1-beta-1 Keywords: proxy model serialization
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 1

Description

When importing data I am running into this error:

Problem installing fixture '../tpm/contacts/fixtures/sampladata.json': Traceback (most recent call last):
  File "/usr/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py", line 153, in handle
    obj.save()
  File "/usr/local/lib/python2.5/site-packages/django/core/serializers/base.py", line 163, in save
    models.Model.save_base(self.object, raw=True)
  File "/usr/local/lib/python2.5/site-packages/django/db/models/base.py", line 495, in save_base
    created=(not record_exists), raw=raw)
UnboundLocalError: local variable 'record_exists' referenced before assignment

Attachments

db.models.base.patch (362 bytes) - added by wardi on 05/24/09 13:20:25.
patch to define record_exists in all cases

Change History

05/24/09 13:20:25 changed by wardi

  • attachment db.models.base.patch added.

patch to define record_exists in all cases

05/24/09 13:33:50 changed by kmtracey

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

It would help if you would provide sample data to recreate the error. If all fixture loading was broken, the test suite would be having serious problems, and it is not. So what is special about you data that is triggering this problem?

05/24/09 14:16:52 changed by dc

I can reproduce this error with this model:

from django.contrib.auth.models import User
class UserProxy(User):
    class Meta:
        proxy = True

and with any fixture, for e.g.

{
	"model": "myapp.userproxy",
	"pk": 1,
	"fields": {
		"username": "username"
	}
}

It also can be repoduced without fixtures:

>>> from myapp.models import UserProxy
>>> UserProxy(username='username').save_base(raw=True)
Traceback (most recent call last):
...
UnboundLocalError: local variable 'record_exists' referenced before assignment

05/24/09 15:31:00 changed by kmtracey

  • needs_better_patch set to 1.
  • needs_tests set to 1.
  • milestone set to 1.1.

OK, so the problem is a combination of using raw=True (as fixture loading (always?) does) and proxy models. It looks to me like the save_base routine is coded to never expect to be called with both raw=True and meta.proxy=True. The attached patch fixes the visible error (avoids an exception) but the resulting behavior of the function does not appear to be correct. It sends a pre-save signal, then skips out on the saving of parent class(es) because raw is True, then skips out on the remainder of the try an update, try an insert logic because meta.proxy is True. The only thing left not under the if not meta.proxy is the sending of the post save signal. So that patch makes it possible to call the routine, and the pre and post save signals will be sent, but nothing will be saved. There seems to be a deeper problem than a local variable used before being set here.

Setting 1.1 milestone because someone with a clue about proxy models and serialization should probably look at this before 1.1.

06/05/09 07:39:46 changed by dc

  • keywords changed from record_exists db import to proxy model serialization.
  • component changed from Uncategorized to Database layer (models, ORM).
  • summary changed from django.db.models.base.py: UnboundLocalError: local variable 'record_exists' referenced before assignment to Saving proxy model with raw=True gives UnboundLocalError 'record_exists'.

06/08/09 08:35:41 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(In [10955]) Fixed #11194 -- Corrected loading of Proxy models from fixtures (and, by extension, save_base(raw=True) for Proxy models).


Add/Change #11194 (Saving proxy model with raw=True gives UnboundLocalError 'record_exists')




Change Properties
Action