Code

Opened 5 years ago

Closed 5 years ago

#10892 closed (wontfix)

ManyToManyField attribute looks up wrong name in Admin

Reported by: jhawk28@… Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords: ManyToManyField
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

ManyToManyField attribute does not look up the correct key in the Admin if the django naming conventions are not followed in a legacy database.

This returns an error like:
ORA-00904: "AUTH_BOOK_MAPPING"."BOOK_ID": invalid identifier
Example:

class Book(models.Model):
  book = models.CharField(max_length=4, primary_key=True, db_column='BOOK')
  description = models.CharField(max_length=250, db_column='DESCRIPTION', blank=True)
  class Meta:
    db_table = u'BOOK_TABLE'

class Author(models.Model):
  auth = models.DecimalField(decimal_places=0, max_digits=38, primary_key=True, db_column='AUTH_ID')
  name = models.CharField(max_length=250, db_column='NAME', blank=True)
  books = models.ManyToManyField(Book, db_table=u'AUTH_BOOK_MAPPING')
  class Meta:
    db_table = u'AUTHOR_TABLE'

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by ramiro

  • milestone 1.1 deleted
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

"AUTH_BOOK_MAPPING"."BOOK_ID is the FK from the intermediate table to the Book model, right?.

I don't think Django can/should introspect the names of these Foreign Keys at runtime. Try creating a model that represent your intermediate table and use Meta.db_table plus the right Foreign Key field names (possibly also using te db_column field option in a way similar to what you 've already done in the Book and Author models). Specifiy that model in the through option for the ManyToManyField.

comment:2 Changed 5 years ago by Alex

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

Ramiro's analysis is correct, if you don't use an intermediary model then django can't guess at the column name.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.