我无法覆盖管理网站中ModelAdmin对象的TabularInline内联中的formset。我知道你应该有一个与TabularInline对象相关联的模型,但我不知道如何在用于生成formset的表单对象上指定它。使用下面的代码,我得到“'AppAssetInline.formset'不会从BaseModelFormSet继承。”
class AppAssetForm(forms.ModelForm):
model = App.assets.through
primary = forms.BooleanField()
uuid = forms.CharField()
class AppAssetInline(admin.TabularInline):
model = App.assets.through
AssetFormset = formset_factory(AppAssetForm)
formset = AssetFormset
class AppAdmin(admin.ModelAdmin):
inlines = [AppAssetInline,]
答案 0 :(得分:0)
您应该使用django.forms.models.inlineformset_factory
代替formset_factory
答案 1 :(得分:0)
我的问题的答案与我如何构建表单没有关系,而是我如何加入模型中的字段。我的模型中有以下结构:
class App(models.Model):
package = models.FileField(upload_to=settings.APP_PACKAGE_ROOT)
assets = models.ManyToManyField('AppAsset', blank=True, null=True)
download_count = models.IntegerField(default=0)
class AppAsset(models.Model):
def __unicode__(self):
return self.asset_file.name
notes = models.CharField(max_length=255, null=True, blank=True)
type = models.CharField(max_length=255, null=True, blank=True)
asset_file = models.FileField(upload_to=settings.APP_PACKAGE_ROOT)
我所做的是更改结构,以便AppAsset现在在App上为其资产提供外键。之后,我可以在AppAsset模型上使用TabularInline而没有任何问题。以下是最新的源文件:
https://github.com/ridecharge/spout/blob/master/Spout/AppDistribution/models.py https://github.com/ridecharge/spout/blob/master/Spout/AppDistribution/admin.py