I googled many times and found only one solution to add custom intermediate model for two model having relation through 3rd model. And I applied as usual as suggested but still getting this issue:
can't include the ManyToManyField field 'terms' because 'terms' manually specifies a 'through' model
class Term(models.Model):
class Meta:
db_table = "tbl_term"
name = models.CharField(max_length=32)
class Post(models.Model):
class Meta:
db_table = "tbl_post"
title = models.CharField(max_length=96)
content = models.TextField()
terms = models.ManyToManyField("Term", through="TermRelation")
class TermRelation(models.Model):
class Meta:
db_table = "tbl_term_relation"
term = models.ForeignKey("Term", db_column="id_term")
post = models.ForeignKey("Post", db_column="id_post")
@admin.register(Term)
class AdminTerm(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['name']})
]
@admin.register(Post)
class AdminPost(admin.ModelAdmin):
fieldsets = [
(None, {'fields':['title', 'content', 'terms']})
]
You have to change the admin to include InlineModelAdmin
objects
So, change
class AdminPost(admin.ModelAdmin):
fieldsets = [
(None, {'fields':['title', 'content', 'terms']})
]
to
class TermInlineAdmin(admin.TabularInline):
model = Post.terms.through
class AdminPost(admin.ModelAdmin):
fieldsets = [
(None, {'fields':['title', 'content']})
]
inlines = (TermInlineAdmin,)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With