简体   繁体   中英

Overriding formset in TabularInline Django admin form

I'm having trouble overriding the formset on a TabularInline inline of a ModelAdmin object in my admin site. I know you're supposed to have a model associated with a TabularInline object, but I'm not sure how to specify this on the form object used to generate the formset. With the code below, I'm getting "'AppAssetInline.formset' does not inherit from 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,]

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

The answer to my question didn't have to do with how I was structuring my forms, but rather how I was joining fields on my models. I had the following structure in my models:

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)

What I did was change the structure such that AppAsset now has a foreign key on App for its assets. After that, I could use the TabularInline on the AppAsset model with no problems. Here are the latest source files:

https://github.com/ridecharge/spout/blob/master/Spout/AppDistribution/models.py https://github.com/ridecharge/spout/blob/master/Spout/AppDistribution/admin.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM