【问题标题】:Overriding formset in TabularInline Django admin form在 TabularInline Django 管理表单中覆盖表单集
【发布时间】:2013-10-27 13:51:54
【问题描述】:

我无法在我的管理站点中覆盖 ModelAdmin 对象的 TabularInline 内联表单集。我知道您应该有一个与 TabularInline 对象关联的模型,但我不确定如何在用于生成表单集的表单对象上指定它。使用下面的代码,我得到“'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,]

【问题讨论】:

    标签: python django django-forms django-admin


    【解决方案1】:

    您应该使用django.forms.models.inlineformset_factory 而不是formset_factory

    【讨论】:

      【解决方案2】:

      我的问题的答案与我如何构建表单无关,而与我如何加入模型中的字段有关。我的模型中有以下结构:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        • 1970-01-01
        • 2018-10-19
        • 2012-07-05
        • 2012-07-09
        • 1970-01-01
        • 2011-01-24
        相关资源
        最近更新 更多