Opened 8 years ago

Closed 7 years ago

#3323 closed (fixed)

Unhelpful error message in case of unresolved string relation ('str' object has no attribute '_meta')

Reported by: alex@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: sprintsept14 sprintdec01
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

imagine the following model:

class Test(models.model):
   rel = models.ForeignKey("bleh")

if the relation is not resolvable, in case of a typo or if it's in a different file, the user gets a rather unhelpful exception:

Unhandled exception in thread started by <function inner_run at 0x2af9aa4e9668>
Traceback (most recent call last):
  File "/home/alex/source/svn/django/django/core/management.py", line 1108, in inner_run
    validate()
  File "/home/alex/source/svn/django/django/core/management.py", line 1070, in validate
    num_errors = get_validation_errors(outfile)
  File "/home/alex/source/svn/django/django/core/management.py", line 936, in get_validation_errors
    for r in rel_opts.get_all_related_objects():
  File "/home/alex/source/svn/django/django/db/models/options.py", line 127, in get_all_related_objects
    if f.rel and self == f.rel.to._meta:
AttributeError: 'str' object has no attribute '_meta'

a quick hacky fix is attached, but i guess it should be done in django.core.management.validate

Attachments (4)

django-string-rel.patch (668 bytes) - added by alex@… 8 years ago.
quick hack
django.db.models.options.rev6263.diff (670 bytes) - added by Axis_of_Entropy 8 years ago.
similar hackish fix for ManyToManyField
patch-foreignkey-attributeerror.diff (2.9 KB) - added by Gregor_Kopka 8 years ago.
Patch updated: fixed the error message to display the correct name of missing model
django-string-rel-with-test.patch (4.7 KB) - added by shaleh 7 years ago.
update to the recent patch, adds tests keeps the behavior consistent

Download all attachments as: .zip

Change History (13)

Changed 8 years ago by alex@…

quick hack

comment:1 Changed 8 years ago by anonymous

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set

comment:2 Changed 8 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Accepted

Thanks Alex for reporting the issue and providing a starting patch.

comment:3 Changed 8 years ago by anonymous

This might be related if not a duplicate of #1662.

Changed 8 years ago by Axis_of_Entropy

similar hackish fix for ManyToManyField

comment:4 Changed 8 years ago by Axis_of_Entropy

  • Keywords sprintsept14 added
  • Needs tests set

I've also encountered this, but with ManyToManyField, and I think that this may describe the root problem of #5311 (see my discussion there)
I've attached a hackish fix that helped my problem.

comment:5 Changed 8 years ago by Gregor_Kopka

  • Patch needs improvement unset

Added a patch wich fixes this without (hopefully) being hackish.
Validation error will be given now as intended - crash with traceback is gone now.

Changed 8 years ago by Gregor_Kopka

Patch updated: fixed the error message to display the correct name of missing model

Changed 7 years ago by shaleh

update to the recent patch, adds tests keeps the behavior consistent

comment:6 Changed 7 years ago by shaleh

  • Keywords sprintdec01 added
  • Needs tests unset

my patch adds tests

comment:7 Changed 7 years ago by Simon G <dev@…>

  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 7 years ago by Simon G <dev@…>

  • Summary changed from Unhelpful error message in case of unresolved string relation to Unhelpful error message in case of unresolved string relation ('str' object has no attribute '_meta')

comment:9 Changed 7 years ago by mtredinnick

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

Fixed in [6835].

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