Opened 13 years ago

Closed 11 years ago

Last modified 11 years ago

#16433 closed Bug (fixed)

Missing 'help_text' in admin form when showing the back reference for a OneToOne field.

Reported by: chris@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: admackin+django@…, net147, Francis Devereux Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm receiving the following exception in the admin pages for a particular construct:

Caught AttributeError while rendering: 'RelatedObject' object has no attribute 'help_text'

Here's the pastebin for the full exception: http://dpaste.com/564587/

The problem, I believe, is that help_text is missing for the admin screens when attempting to show a "read only" field of an object that was automatically generated from a OneToOneField.

Here's a reduced form of the models and admin pages that cause the error. The application is an art show management software, that has artists, pieces, invoices, and a class called "invoice item" that will attach a piece to an invoice with a price associated. A piece doesnt have a "sale price" until it gets attached to an invoice, a piece can only be on one invoice, but an invoice can have many pieces:

class Piece ( models.Model ):
    # most detail omitted
    name = models.CharField ( max_length = 100 )

class Invoice ( models.Model ):
    # most detail omitted
    total_paid = models.DecimalField ( max_digits=7, decimal_places=2, blank=True, null=True )

class InvoiceItem ( models.Model ):
    piece = models.OneToOneField ( Piece )
    invoice = models.ForeignKey ( Invoice )
    price = models.DecimalField ( max_digits=7, decimal_places=2 )
    def __unicode__ ( self ):
        return "%s for $%s" % ( self.invoice, self.price )

then in admin.py:

class PieceAdmin ( admin.ModelAdmin ):
    # most detail omitted
    fields = ( 'artist', 'pieceid', 'name', 'location', 'not_for_sale', 'adult', 'min_bid', 'buy_now', 'voice_auction', 'bidsheet_scanned', 'status', 'top_bid', 'invoiceitem' )
    readonly_fields = ( 'top_bid', 'invoiceitem' )

Most of the "fields" relate to fields that were omitted in the models above, or are methods of the PieceAdmin class. Of note is the 'invoiceitem' field. This is a field automatically generated for a Piece because of the OneToOneField field on InvoiceItem. Having this as a "readonly_fields" makes it available to the model (that seems to be a requirement), however, the presence of this at all generates the above exception. This code _did_ work in 1.3. I suspect that help_text is new for 1.4, but not all possible combinations of this or that have had the help_text added. The above is a bit of a corner-case, I know :)

I can probably work around this bug by creating an invoiceitem method (in much the same way I created a top_bid method).

Attachments (2)

django-16433.patch (498 bytes ) - added by admackin@… 13 years ago.
Patch against r17665
django-16433-with-test.patch (3.3 KB ) - added by Andy MacKinlay 13 years ago.
Same patch as previous with unit tests added

Download all attachments as: .zip

Change History (14)

comment:1 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

by admackin@…, 13 years ago

Attachment: django-16433.patch added

Patch against r17665

comment:2 by admackin@…, 13 years ago

Has patch: set

comment:3 by Claude Paroz, 13 years ago

Needs tests: set

comment:4 by Andy MacKinlay, 13 years ago

Cc: admackin+django@… added

The same problem occurs for ManyToMany fields (for the backwards reference), but only if the related_name attribute is used.

by Andy MacKinlay, 13 years ago

Same patch as previous with unit tests added

comment:5 by Andy MacKinlay, 13 years ago

The test case I've supplied here covers the ManyToMany case.

However, I'm not sure if my fix is treating the symptom instead of the cause.

comment:6 by Andy MacKinlay, 13 years ago

Needs tests: unset

comment:7 by Chris Wilson, 13 years ago

Please can we make help_text_for_field() a method of AdminReadonlyField instead of a static helper method, so that we can override it without monkey-patching?

comment:8 by net147, 12 years ago

Cc: net147 added

comment:9 by anonymous, 12 years ago

In Django v 1.5.1 same Bug

comment:10 by Francis Devereux, 11 years ago

Cc: Francis Devereux added

comment:11 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In af953c45cc56df28d02675e196eca1b54ce28a43:

Fixed #16433 -- Fixed a help_text/read only field interaction that caused an admin crash.

Thanks chris at cogdon.org for the report and admackin for the patch.

comment:12 by Tim Graham <timograham@…>, 11 years ago

In ef1259342b46cd4b63678a4acddf2a2b631d342c:

[1.6.x] Fixed #16433 -- Fixed a help_text/read only field interaction that caused an admin crash.

Thanks chris at cogdon.org for the report and admackin for the patch.

Backport of af953c45cc from master

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