我尝试将GenericTabularInline添加到相关模型的管理员,并且Generic Relation模型的ManyToMany字段不显示任何内容。
models.py
class RelatedIngredients(models.Model):
content_type = models.ForeignKey(ContentType, verbose_name='Content Type')
object_id = models.PositiveIntegerField(verbose_name='Object ID')
content_object = fields.GenericForeignKey('content_type', 'object_id')
ingredients = models.ManyToManyField(Ingredients)
admin.py
class IngredientInline(GenericTabularInline):
model = RelatedIngredients
extra = 0
min_num = 1
max_num = 1
can_delete = False
fields = ['ingredients',]
filter_horizontal = ['ingredients',]
@admin.register(Cake)
class CakeAdmin(admin.ModelAdmin):
fields = ['name', 'description']
inlines = [IngredientInline,]
出现正确的过滤器水平小部件,但它显示为空。没有任何可用或选择的方面。现在我知道模型的工作原理是我尝试将 RelatedIngredients.ingredients 作为外键并且GenericTabularInline工作。
区别是什么?有什么想法吗?