简体   繁体   中英

cannot add inline to django site admin framework

The admin.py is as follows :-

class SiteDetailInline(admin.TabularInline):
    model = SiteDetail 
    form = SiteDetailForm
    fields = ('name', )
    can_delete = False
    extra = 1
    max_num = 1

    def get_readonly_fields(self, request, obj=None):
       if obj:
           return ('clmsid',) + self.readonly_fields
       return self.readonly_fields

class SiteAdmin(admin.ModelAdmin):
    inlines = [ SiteDetailInline, ]
    def queryset(self, queryset):
        return Site.objects.filter(~Q(id = settings.SITE_ID))
    signals.post_save.connect(create_sites_default_user, sender=Site)

admin.site.unregister(Site)
admin.site.register(Site, SiteAdmin)

The models.py is as follows :-

class SiteDetail(models.Model):
    name = models.CharField(max_length=100, unique=True)
    client = models.ForeignKey(client)
    site = models.ForeignKey(Site)
    clmsid = models.CharField(max_length=15, unique=True, verbose_name='clms id', help_text='clms identifier', ) # unique identifier L-XXXXXX-id

    def save(self, *args, **kwargs):
        if "L-" != self.clmsid[:2]:
            self.clmsid = "%s-%s-%s" % ("L", self.accountid, self.id)
        super(SiteDetail, self).save(*args, **kwargs)

    def __unicode__(self):
        return u''

I want to show the extra site details inline in the admin for the site framework. It is not giving any error. However the site details are not displayed inline. Please let me know, what mistake am I doing. Thanks in advance.

Try this

def get_fields(self, request, obj=None):
    if obj:
       return ('clmsid',) + self.fields
    return self.fields

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