我有一个带有外键 且没有db_constraint的模型。我这样做是为了插入可能不指向外部表的值。我能够正确导入值。
在Django管理员中,如果我查看包含无约束外键的模型类型,则看不到模型,但确实看到了一定数量的模型。
这是我的代码
class HlaType(models.Model):
specimen = models.ForeignKey(SpecimenInfo, on_delete=models.CASCADE, verbose_name="Specimen",
related_name="hlatype", db_constraint=False)
allele_key = models.CharField(max_length=5, db_index=True)
allele = models.CharField(max_length=50, blank=True, db_index=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=('specimen', 'allele_key'), name='targets_hlatype_unique_specimen_and_key')
]
class HlaTypeAdmin(ImportExportMixin, admin.ModelAdmin):
raw_id_fields = ["specimen"]
list_display = ('specimen', 'allele_key', 'allele')
resource_class = HlaTypeResource
list_per_page = 200
class HlaTypeResource(ModelResource):
specimen = Field(attribute='specimen', column_name='specimen_id', widget=HlaWidget(SpecimenInfo, 'pk'))
allele_key = Field(attribute='allele_key', column_name='allele_key')
allele = Field(attribute='allele', column_name='allele')
class Meta:
model = HlaType
import_id_fields = ('specimen', 'allele_key')
skip_unchanged = True
fields = ('id', 'specimen', 'allele_key', 'allele')
有什么办法可以解决这个问题吗?