我有这样的外键关系
class Product(models.Model):
brochure = models.ForeignKey(MediaFile)
因为我的库中有很多 MediaFile,我通常使用 aForeignKeyRawIdWidget
来减少查询次数。
但显然在内联模型上使用“ForeignKeyRawIdWidget”不会影响这一点。它仍然执行很多查询。
有人知道为什么吗?
行政:
class ProductInline(admin.StackedInline):
model = models.Product
prepopulated_fields = {"slug": ("title",)}
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'brochure':
kwargs['widget'] = ForeignKeyRawIdWidget(db_field.rel)
return super(ProductInline, self).formfield_for_dbfield(db_field, **kwargs)
对比
class ProductAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'brochure':
kwargs['widget'] = ForeignKeyRawIdWidget(db_field.rel)
return super(ProductAdmin, self).formfield_for_dbfield(db_field, **kwargs)