#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)
Change History (15)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
| milestone: | 1.0 beta → 1.0 |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 17 years ago
| Component: | Contrib apps → Core framework |
|---|---|
| Keywords: | ForeignKey foreign key added; Site removed |
| Owner: | changed from to |
| Status: | new → assigned |
| Summary: | Site Model breaks help() built-in python function → Built-in python function, help(), does not work on models with ForeignKey fields |
comment:4 by , 17 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:5 by , 17 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 , 17 years ago
| Attachment: | help_fixes_with_foreignkeys_r8521.diff added |
|---|
Patch that fixes problem, but with a wrong approach
comment:6 by , 17 years ago
| Triage Stage: | Accepted → Design decision needed |
|---|
comment:7 by , 17 years ago
| Triage Stage: | Design decision needed → 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 by , 17 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 , 17 years ago
| milestone: | 1.0 → 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 by , 17 years ago
| Cc: | added |
|---|
comment:11 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
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.