#16433 closed Bug (fixed)
Missing 'help_text' in admin form when showing the back reference for a OneToOne field.
| Reported by: | 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)
Change History (14)
comment:1 by , 14 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
by , 14 years ago
| Attachment: | django-16433.patch added |
|---|
comment:2 by , 14 years ago
| Has patch: | set |
|---|
comment:3 by , 14 years ago
| Needs tests: | set |
|---|
comment:4 by , 14 years ago
| Cc: | added |
|---|
The same problem occurs for ManyToMany fields (for the backwards reference), but only if the related_name attribute is used.
by , 14 years ago
| Attachment: | django-16433-with-test.patch added |
|---|
Same patch as previous with unit tests added
comment:5 by , 14 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 , 14 years ago
| Needs tests: | unset |
|---|
comment:7 by , 14 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 , 13 years ago
| Cc: | added |
|---|
comment:10 by , 12 years ago
| Cc: | added |
|---|
comment:11 by , 12 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Patch against r17665