Opened 16 years ago

Closed 15 years ago

Last modified 13 years ago

#11194 closed (fixed)

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

Reported by: wardi Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1-beta
Severity: Keywords: proxy model serialization
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

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 (1)

db.models.base.patch (362 bytes ) - added by wardi 16 years ago.
patch to define record_exists in all cases

Download all attachments as: .zip

Change History (7)

by wardi, 16 years ago

Attachment: db.models.base.patch added

patch to define record_exists in all cases

comment:1 by Karen Tracey, 16 years ago

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?

comment:2 by dc, 16 years ago

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

comment:3 by Karen Tracey, 16 years ago

milestone: 1.1
Needs tests: set
Patch needs improvement: set

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.

comment:4 by dc, 15 years ago

Component: UncategorizedDatabase layer (models, ORM)
Keywords: proxy model serialization added; record_exists db import removed
Summary: django.db.models.base.py: UnboundLocalError: local variable 'record_exists' referenced before assignmentSaving proxy model with raw=True gives UnboundLocalError 'record_exists'

comment:5 by Russell Keith-Magee, 15 years ago

Resolution: fixed
Status: newclosed

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

comment:6 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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