Opened 5 years ago

Closed 5 years ago

#12209 closed (fixed)

ManyToManyField.through does not work as the model property of an inline when specified using a string

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

Description

Using the following models:

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author, through='AuthorsBooks')

class AuthorsBooks(models.Model):
    author = models.ForeignKey(Author)
    book = models.ForeignKey(Book)

... with this admin:

class AuthorsInline(admin.TabularInline):
    model = models.Book.authors.through

class BookAdmin(admin.ModelAdmin):
    inlines = [AuthorsInline]
    exclude = ('authors',)

admin.site.register(models.Book, BookAdmin)

... does not function. If you change the models to:

class AuthorsBooks(models.Model):
    author = models.ForeignKey('Author')
    book = models.ForeignKey('Book')

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author, through=AuthorsBooks)

... the admin works just fine. This seems to be an issue with specifying Book.authors.through as a string instead of using the model class itself.

Change History (2)

comment:1 Changed 5 years ago by dgouldin

  • Cc dgouldin@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by russellm

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

(In [11736]) Fixed #12209 -- Made the through attribute on a m2m relation into a property to ensure that the fully resolved through model is always provdided. Thanks to dgouldin for the report.

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