Django admin中的True值不可编辑复选框

时间:2014-03-25 14:01:18

标签: python django forms checkbox

模型

class Company(BaseModel): 
    rag_soc = models.CharField(_("Ragione Sociale"), max_length=255, blank=False, null=False)
    sure = models.BooleanField(_("Continua Modifiche"), default= False)
    p_iva = models.CharField(_("Partita IVA/Codice Fiscale"), max_length=16, blank=True, null=True, unique = True)
    ref_name_1 = models.CharField(_('Nome Referente'), max_length=70, blank=False, null=False)
    ref_name_2 = models.CharField(_('Nome Referente'), max_length=70, blank=True, null=True)
    compentecy = models.CharField(_("Competenza"), max_length=255, choices=COMPETENCY, blank=False)
    commodity_sector = models.CharField(_("Settore Merceologico"), max_length=255, choices=ATECO, blank=True, null=True)
    employees = models.IntegerField(_('Numero Dipendenti'), max_length=4, blank=True, default = 0 )

class OutCome(BaseModel):
    outcome = models.CharField(_("Esito"), max_length=25, choices=OUTCOME, blank=False, null=True)
    out_date = models.DateTimeField(_("In Data"), blank=True, null=True)
    return_date = models.BooleanField(_('Richiamata'), default=False,)
    notes = models.TextField(_("Note"), max_length=512, blank=True, null=True)
    seller = models.BooleanField(_('Richiesta Venditore'), default=False, blank = True)
    company = models.ForeignKey(Company, verbose_name=_('Azienda'), related_name='company_set')

管理员

class CompanyParent(admin.ModelAdmin):
    change_form_template = 'admins/change_form_company.html'

    list_display = ('rag_soc', 'city', 'return_user', 'lastMod')
    search_fields = ('rag_soc', 'p_iva', 'city', 'seller__name', 'seller__surname')
    list_filter = ('compentecy', 'seller', 'user', 'created', 'conf_company', 'prov_conf_company')

    form = CompanyForm
    list_per_page = 55
    inlines = [OutComeOffersList, OutComeList, OutComeAdd]

class OutComeList(PaginationInline):
    order_by = ('-created',)
    form = OutcomeForm
    model = OutCome
    can_delete = False
    extra = 1
    verbose_name = 'Contatto Precedente'
    verbose_name_plural = 'Contatti Precedenti'
    readonly_fields = ['outcome', 'out_date', 'created', 'seller', 'notes',]

在OutCome内联的每个实例中,如果实例为True,我需要显示绿色复选标记,如果它为False则需要显示空复选框。 我该怎么做?感谢

1 个答案:

答案 0 :(得分:0)

您可以根据return_date字段显示不同的内容

models.py

class OutCome(BaseModel):
    ...
    return_date = models.BooleanField(_('Richiamata'), default=False,)
    ...
    def return_date_ctrl(self):
        if self.return_date: # if is true
            return u'<img src="/media/checkmark.png" />'
        else:
            # you can use js & ajax to control this
            return u'<input type="checkbox" id="id_out_date" name="out_date" />'
    return_date_ctrl.short_description = 'return date'
    return_date_ctrl.allow_tags = True

admin.py

class OutComeList(PaginationInline):
    ...
    # add return_date_ctrl to readonly_fields
    readonly_fields = ['return_date_ctrl', ...]

希望我提供的路线可以帮到你!