Opened 16 years ago

Closed 15 years ago

Last modified 15 years ago

#8248 closed (fixed)

Built-in python function, help(), does not work on models with ForeignKey fields

Reported by: lingrlongr Owned by:
Component: Core (Other) Version: dev
Severity: Keywords: help ForeignKey foreign key
Cc: ironfroggy@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a model refers to a Site model, the help() function stops working, meaning functions, docstrings, etc don't print.

To reproduce:

$ django-admin.py startproject myproj
$ ./manage.py shell
>>> from django.contrib.sites.models import Site
>>> help(Site)

This will return correct functions, docstrings, etc...

Now create an app:

$ ./manage.py startapp myapp

Create a model:

from django.db import models
from django.contrib.sites.models import Site

class MyModel(models.Model):
    name = models.CharField(max_length=50)
    site = models.ForeignKey(Site)

Now try to view help for this either MyModel or Site:

$ ./manage.py shell
>>> from myapp.models import MyModel
>>> from django.contrib.sites.models import Site
>>> help(MyModel)
>>> help(Site)

Both calls will only show something similar to:

Help on class MyModel in module myproj.myapp.models:

MyModel = <class 'myproj.myapp.models.MyModel'>
(END)

Tested on SVN 8204 and 8313 - same results

Attachments (1)

help_fixes_with_foreignkeys_r8521.diff (1.8 KB ) - added by Manuel Saelices 16 years ago.
Patch that fixes problem, but with a wrong approach

Download all attachments as: .zip

Change History (15)

comment:1 by lingrlongr, 16 years ago

comment:2 by Malcolm Tredinnick, 16 years ago

milestone: 1.0 beta1.0
Triage Stage: UnreviewedAccepted

comment:3 by lingrlongr, 16 years ago

Component: Contrib appsCore framework
Keywords: ForeignKey foreign key added; Site removed
Owner: changed from nobody to lingrlongr
Status: newassigned
Summary: Site Model breaks help() built-in python functionBuilt-in python function, help(), does not work on models with ForeignKey fields

I dug a little deeper. Apparently this problem exists for any model that has a relationship to another model. For example, a Comment model would have a foreign key relationship to a Blog model. help(Comment) would fail as indicated above.

I updated some of the bug properties as its not related to the Site model after all.

comment:4 by lingrlongr, 16 years ago

Owner: lingrlongr removed
Status: assignednew

comment:5 by Manuel Saelices, 16 years ago

The error is due to inspect python builtin module. When this module introspect model class with a ForeignKey, try to get field without passing a instance.

Django related fields raises an AttributeError and inspect module pass this error and only write class name.

Problem is that fix is backwards incompatible, and also changes behaviour in models. I will attach a patch thats fixes problem, but I'm not sure if is a good idea commit this ;-)

by Manuel Saelices, 16 years ago

Patch that fixes problem, but with a wrong approach

comment:6 by Manuel Saelices, 16 years ago

Triage Stage: AcceptedDesign decision needed

comment:7 by Malcolm Tredinnick, 16 years ago

Triage Stage: Design decision neededAccepted

There's no "design decision needed" here -- the ticket is already accepted as a bug, although it could conceivably be pushed to post-1.0 if we don't have time, since it's not really a showstopper.

However, you're right about the patch being a little suspect. It probably has little chance of being applied as is. It removes an important safety net and error check just to make help() work. That's not a good trade-off in my mind.

Excellent debugging, though. You've worked out precisely why the problem occurs, so we can try to think of some way to work around it.

comment:8 by Jacob, 16 years ago

I'm actually fine with the change: I've thought for a while that Model.field ought to return a Field instead of raising an AttributeError. Making this work for regular fields -- those without associated descriptors -- is actually a bit annoying, but I don't see that we need to make the change all at once.

So the only question in my mind is weather we sneak this in for 1.0, or push it off. I'd be inclined to leave the current behavior -- broken as it is -- for later so we can make a consistent change for all fields.

comment:9 by Malcolm Tredinnick, 16 years ago

milestone: 1.0post-1.0

Let's punt to after 1.0 just to avoid opening up another avenue for bugs just at the moment. Will be backwards-compatible to change afterwards.

comment:10 by ironfroggy, 16 years ago

Cc: ironfroggy@… added

comment:11 by Luke Plant, 15 years ago

Resolution: fixed
Status: newclosed

(In [9550]) Fixed #8248: made help() work on models and improved introspection support.

Descriptors now return themselves when accessed via the class, as per standard
Python descriptors like property().

comment:12 by Luke Plant, 15 years ago

(In [9562]) Refs #8248 - GenericRelations descriptors now return self when accessed via class

These were missed in r9550

comment:13 by Luke Plant, 15 years ago

(In [9634]) [1.0.X] Fixed #8248: made help() work on models and improved introspection support.

Descriptors now return themselves when accessed via the class, as per standard
Python descriptors like property().

Backported from r9550 and also r9562 and r9563

comment:14 by (none), 15 years ago

milestone: post-1.0

Milestone post-1.0 deleted

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