Opened 7 years ago

Closed 7 years ago

Last modified 6 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: master
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: UI/UX:

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 msaelices 7 years ago.
Patch that fixes problem, but with a wrong approach

Download all attachments as: .zip

Change History (15)

comment:1 Changed 7 years ago by lingrlongr

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

comment:2 Changed 7 years ago by mtredinnick

  • milestone changed from 1.0 beta to 1.0
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 7 years ago by lingrlongr

  • Component changed from Contrib apps to Core framework
  • Keywords ForeignKey foreign key added; Site removed
  • Owner changed from nobody to lingrlongr
  • Status changed from new to assigned
  • Summary changed from Site Model breaks help() built-in python function to Built-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 Changed 7 years ago by lingrlongr

  • Owner lingrlongr deleted
  • Status changed from assigned to new

comment:5 Changed 7 years ago by msaelices

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 ;-)

Changed 7 years ago by msaelices

Patch that fixes problem, but with a wrong approach

comment:6 Changed 7 years ago by msaelices

  • Triage Stage changed from Accepted to Design decision needed

comment:7 Changed 7 years ago by mtredinnick

  • Triage Stage changed from Design decision needed to Accepted

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

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

  • milestone changed from 1.0 to post-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 Changed 7 years ago by ironfroggy

  • Cc ironfroggy@… added

comment:11 Changed 7 years ago by lukeplant

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

(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 Changed 7 years ago by lukeplant

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

These were missed in r9550

comment:13 Changed 7 years ago by lukeplant

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

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

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