Opened 16 years ago

Closed 16 years ago

#8681 closed (invalid)

Uncaught exception in django/db/models/base.py: UnboundLocalError

Reported by: HM <hanne.moa@…> Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords: models, onetoonefield
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In source:django/trunk/django/db/models/base.py as of [8696], lines 197-203:

        for field in fields_iter:
            rel_obj = None
            if kwargs:
                if isinstance(field.rel, ManyToOneRel):
                    try:
                        # Assume object instance was passed in.
                        rel_obj = kwargs.pop(field.name)

If kwargs[field.name] is None, rel_obj is set to None, val is not set at all, and we get an UnboundLocalError at line 226. Context: the model in question had a OneToOneField whose primary_key was not yet set/was set to None and a save() was attempted.

See stacktrace at dpaste: http://dpaste.com/74655/

Either we need to have a better error-message here, for instance mentioning what field is the problem and how it can be fixed, or the code needs to do something sensible.

Change History (3)

comment:1 by Malcolm Tredinnick, 16 years ago

You've leapt right into explaining where you've worked out the problem lies, but haven't actually explained how this occurs. What are the steps to repeat the problem? The situation you talk about with a OneToOneField basically describes how saving works for inherited models and that works (it's even in the test suite).

Since you have a failing case in mind, can you show a few lines of code that demonstrate this problem so we don't have to reverse-engineer how to make it happen. Thanks.

comment:2 by anonymous, 16 years ago

Ah. I hadn't discovered the cause in my own code when I made the ticket.

parent_model = None
object, created = ModelWithOneToOneField.objects.get_or_create(theonetoonefield=parent_model)

This is renamed from the original version. "parent_model" is None because of a screw up when attempting to save it for the first time.

"theonetoonefield" was also set as the primary key of ModelWithOneToOneField, so very understandable that *something* went wrong.

comment:3 by Jacob, 16 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top