Opened 10 years ago

Closed 9 years ago

#8681 closed (invalid)

Uncaught exception in django/db/models/ UnboundLocalError

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


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

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

If kwargs[] 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:

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 Changed 10 years ago by Malcolm Tredinnick

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 Changed 10 years ago by anonymous

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 Changed 9 years ago by Jacob

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