Opened 4 years ago

Closed 2 years ago

Last modified 2 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: master
Severity: Normal Keywords:
Cc: admackin+django@…, net147, frankoid 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@… 3 years ago.
Patch against r17665
django-16433-with-test.patch (3.3 KB) - added by admackin 3 years ago.
Same patch as previous with unit tests added

Download all attachments as: .zip

Change History (14)

comment:1 Changed 4 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Changed 3 years ago by admackin@…

Patch against r17665

comment:2 Changed 3 years ago by admackin@…

  • Has patch set

comment:3 Changed 3 years ago by claudep

  • Needs tests set

comment:4 Changed 3 years ago by admackin

  • Cc admackin+django@… added

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

Changed 3 years ago by admackin

Same patch as previous with unit tests added

comment:5 Changed 3 years ago by admackin

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

  • Needs tests unset

comment:7 Changed 3 years ago by gcc

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

  • Cc net147 added

comment:9 Changed 2 years ago by anonymous

In Django v 1.5.1 same Bug

comment:10 Changed 2 years ago by frankoid

  • Cc frankoid added

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

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

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 Changed 2 years ago by Tim Graham <timograham@…>

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