检查条件后,Django会填充管理字段列表

时间:2016-05-19 13:29:16

标签: django django-models django-forms django-admin django-views

我正在检查字段的值是否说short_text1在assosiative模型中等于1然后我填充 LabelAdmin 字段列表以在管理界面上显示字段short_text1,但这似乎不是工作。我使用get_object_or_404的原因是因为 Assosiative 模型中会有很多字段,我希望管理员只显示已经检查为true的字段 Assosiative 模型。

admin.py

    from django.shortcuts import get_object_or_404
    from django.contrib import admin
    from .models import Master, Label, Assosiative, Contact, Project
    from .forms import get_field_label

    class LabelAdmin(admin.ModelAdmin):
        #LabelAdminForm= get_field_label
        #form = LabelAdminForm
        l_asso = get_object_or_404(Assosiative, slug=slug)
        fields_list = []
        if l_asso.short_text1 == 1:
            fields_list.append('short_text1')
        if l_asso.short_text2 == 1:
            fields_list.append('short_text2')
        if l_asso.short_text3 == 1:
            fields_list.append('short_text3')
        if l_asso.short_text4 == 1:
           fields_list.append('short_text4')
        if l_asso.short_text5 == 1:
           fields_list.append('short_text5')
        fields = fields_list
        prepopulated_fields = {'slug':['assosiative']}
        class Meta:
            model = Label


    admin.site.register(Label, LabelAdmin)

models.py assosiative model

    class Assosiative(models.Model):
         form_name = models.CharField(max_length=100, unique=True)
         form_description = models.CharField(max_length=200)
         slug = models.SlugField(unique=True)
         instructions = models.TextField(max_length=500, blank=True, null=True)
         heading1 = models.TextField(max_length=500, blank=True, null=True)
         short_text1 = models.BooleanField(default=False)
         short_text2 = models.BooleanField(default=False)
         short_text3 = models.BooleanField(default=False)
         short_text4 = models.BooleanField(default=False)
         short_text5 = models.BooleanField(default=False)

models.py标签模型

      class Label(models.Model):
           assosiative = models.OneToOneField(Assosiative, unique=True)
           slug = models.SlugField(unique=True)
           short_text1 = models.CharField(max_length=150, blank=True, null=True)
           short_text2 = models.CharField(max_length=150, blank=True, null=True)
           short_text3 = models.CharField(max_length=150, blank=True, null=True)
           short_text4 = models.CharField(max_length=150, blank=True, null=True)
           short_text5 = models.CharField(max_length=150, blank=True, null=True)

1 个答案:

答案 0 :(得分:0)

您应该使用ModelAdmin的{​​{3}}方法。您无需使用get_object_or_404来获取关联,因为它已与传递给get_fields方法的当前对象链接。

class LabelAdmin(admin.ModelAdmin):
    def get_fields(self, request, obj=None):
        if obj is None:
            return super(LabelAdmin, self).get_fields(request, obj)
        fields = self.fields or list()
        fields = list(fields)    # to make copy of original list
        for x in xrange(1, 6):
            fields.remove('short_text{}'.format(x))
        if obj.assosiative.short_text1 == 1:
            fields.append('short_text1')
        if obj.assosiative.short_text2 == 1:
            fields.append('short_text2')
        if obj.assosiative.short_text3 == 1:
            fields.append('short_text3')
        if obj.assosiative.short_text4 == 1:
            fields.append('short_text4')
        if obj.assosiative.short_text5 == 1:
            fields.append('short_text5')
        return fields    
    class Meta:
        model = Label