Code

Opened 6 years ago

Closed 5 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: 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:

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.

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 6 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 5 years ago by jacob

  • Resolution set to invalid
  • Status changed from new to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.