我正在使用ArrayReferenceField
而不是ManyToManyField
因为我正在使用djongo
我的数据库连接器。我希望这个字段在 django 管理站点中显示为内联模型。但我得到了错误AttributeError: 'ArrayReferenceDescriptor' object has no attribute 'through'
。我知道这ArrayReferenceField
不会在数据库中创建中间模型。那么如何在 django admin 中成功地将该字段显示为内联?
我的模型:
class Synonym(models.Model):
base_name = models.CharField(
blank=False,
max_length=5000,
verbose_name='Base Entity Value',
help_text='The entity value for which you want to provide the synonym.',
)
extra_name = models.CharField(
blank=False,
max_length=5000,
verbose_name='A synonym',
help_text='Synonyms of the base entity value.',
)
class Meta:
# abstract = True
ordering = ['base_name']
def __str__(self):
return self.base_name
class qa(DirtyFieldsMixin, models.Model):
synonyms = models.ArrayReferenceField(
blank=True,
null=True,
to=Synonym,
on_delete=models.SET_NULL,
help_text='Synonyms of the entities of the question.',
)
在 admin.py 中:
class SynonymInline(admin.TabularInline):
model = qa.synonyms.through
class SynonymAdmin(admin.ModelAdmin):
inlines = [
SynonymInline,
]
@admin.register(qa)
class qaAdmin(admin.ModelAdmin):
# form = qaModelForm
list_display = ('question', 'answer', 'module', 'intent', 'display_entities_name')
list_filter = ('module', 'intent', 'username',)
inlines = [
SynonymInline,
]