Opened 18 years ago

Closed 17 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: dev
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: no UI/UX: no

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@… 18 years ago.
quick hack
django.db.models.options.rev6263.diff (670 bytes ) - added by Adam Vollrath 17 years ago.
similar hackish fix for ManyToManyField
patch-foreignkey-attributeerror.diff (2.9 KB ) - added by Gregor_Kopka 17 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 17 years ago.
update to the recent patch, adds tests keeps the behavior consistent

Download all attachments as: .zip

Change History (13)

by alex@…, 18 years ago

Attachment: django-string-rel.patch added

quick hack

comment:1 by anonymous, 18 years ago

Has patch: set
Patch needs improvement: set

comment:2 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedAccepted

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

comment:3 by anonymous, 17 years ago

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

by Adam Vollrath, 17 years ago

similar hackish fix for ManyToManyField

comment:4 by Adam Vollrath, 17 years ago

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 by Gregor_Kopka, 17 years ago

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.

by Gregor_Kopka, 17 years ago

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

by shaleh, 17 years ago

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

comment:6 by shaleh, 17 years ago

Keywords: sprintdec01 added
Needs tests: unset

my patch adds tests

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

Triage Stage: AcceptedReady for checkin

comment:8 by Simon G <dev@…>, 17 years ago

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

comment:9 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

Fixed in [6835].

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