本文介绍了Django的:管理:改变字段的管理小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的一个布尔值模型:

I have a model with a boolean value like that:

class TagCat(models.Model):
    by_admin = models.BooleanField(default=True)

这表现为在管理一个复选框。

This appears as a checkbox in admin.


  1. 我怎么会用这个作为管理一个单选按钮?

  2. 另外,我怎么把它变成总是与管理有一定选择的价值呢?

  3. 另外,我想的缺省值是相反的,当一个非管理员用户添加 TagCat 。此字段应该从他身上隐藏。

  1. How could I use this as a radio button in admin?
  2. Also, how do I make it be always with a certain selected value in admin?
  3. Also, I want the default value to be the opposite, when a non-admin user adds a TagCat. This field should be hidden from him.

谁能告诉我如何做到这一点? Django文档似乎没有在这样的细节中去。

Can someone tell me how to do this? Django documentation doesn't seem to go in such details.

推荐答案

更​​新1:code,它让我与1完成)(不要忘了TOT的选择传递给BooleanField模型)

UPDATE 1: Code that gets me done with 1) (don't forget tot pass CHOICES to the BooleanField in the model)

from main.models import TagCat
from django.contrib import admin
from django import forms

class MyTagCatAdminForm(forms.ModelForm):
    class Meta:
        model = TagCat
        widgets = {
          'by_admin':forms.RadioSelect
        }

class TagCatAdmin(admin.ModelAdmin):
    form = MyTagCatAdminForm


admin.site.register(TagCat, TagCatAdmin)

单选按钮出现丑陋和流离失所,但至少,他们的工作

The radio buttons appear ugly and displaced, but at least, they work

2)我在MyModel.py以下信息解决:

2) I solved with following info in MyModel.py:

BYADMIN_CHOICES = (
    (1, "Yes"),
    (0, "No"),
)
class TagCat(models.Model):
    by_admin = models.BooleanField(choices=BYADMIN_CHOICES,default=1)

这篇关于Django的:管理:改变字段的管理小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:46