我使用ArrayReferenceField
而不是ManyToManyField
,因为我使用djongo
作为数据库连接器。我希望此字段在django管理站点中显示为内联模型。但是我收到错误AttributeError: 'ArrayReferenceDescriptor' object has no attribute 'through'
。我知道ArrayReferenceField
不会在数据库中创建中间模型。那么如何在django管理员中成功显示为内联字段?
我的模特:
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,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','username',)
inlines = [
SynonymInline,]