Django

Code

Ticket #8248 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

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

Reported by: lingrlongr Assigned to:
Milestone: Component: Core framework
Version: SVN Keywords: help ForeignKey foreign key
Cc: ironfroggy@gmail.com Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

help_fixes_with_foreignkeys_r8521.diff (1.8 kB) - added by msaelices on 08/24/08 18:13:02.
Patch that fixes problem, but with a wrong approach

Change History

08/11/08 23:15:35 changed by lingrlongr

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

08/11/08 23:19:07 changed by mtredinnick

  • stage changed from Unreviewed to Accepted.
  • milestone changed from 1.0 beta to 1.0.

08/12/08 13:24:41 changed by lingrlongr

  • keywords changed from Site help to help ForeignKey foreign key.
  • owner changed from nobody to lingrlongr.
  • status changed from new to assigned.
  • component changed from Contrib apps to Core framework.
  • 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.

08/12/08 13:25:16 changed by lingrlongr

  • owner deleted.
  • status changed from assigned to new.

08/24/08 18:11:07 changed 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 ;-)

08/24/08 18:13:02 changed by msaelices

  • attachment help_fixes_with_foreignkeys_r8521.diff added.

Patch that fixes problem, but with a wrong approach

08/24/08 18:13:25 changed by msaelices

  • stage changed from Accepted to Design decision needed.

08/24/08 19:48:49 changed by mtredinnick

  • 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.

08/25/08 13:04:00 changed 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.

08/25/08 13:12:03 changed 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.

10/25/08 06:55:02 changed by ironfroggy

  • cc set to ironfroggy@gmail.com.

12/02/08 16:09:51 changed by lukeplant

  • status changed from new to closed.
  • resolution set to fixed.

(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().

12/03/08 09:45:20 changed by lukeplant

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

These were missed in r9550

12/09/08 18:14:56 changed 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

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted


Add/Change #8248 (Built-in python function, help(), does not work on models with ForeignKey fields)




Change Properties
Action