简体   繁体   中英

How do i replace the charfield with a checkbox in django admin

ok, here goes. The tapStatus variable is used to decide whether or not this item gets pasted onto the website - it's a simple on off. I'm trying to make the admin page more user-friendly - right now I just use the charfield to accept a 1 or a 0, but a checkbox would be more appropriate. Can I modify the admin page to show a checkbox instead of the text box?

class beer(models.Model):
tapStatus = models.CharField(max_length=1)
def __unicode__(self):
    return self.tapStatus
beerdescription = models.CharField(max_length=400)
def __unicode__(self):
    return self.beerdescription
beerStyle = models.CharField(max_length=5)
def __unicode__(self):
    return self.beerStyle
beerabv = models.CharField(max_length=5)
def __unicode__(self):
    return self.beerabv
beername = models.CharField(max_length=40)
def __unicode__(self):
    return self.beername

You can use BooleanField . Here is an example:

class beer(models.Model):
    tapStatus = models.BooleanField(_('tap status'), default=False,
            help_text=_('decide whether or not this item gets pasted onto the website'))
    def __unicode__(self):
        return str(self.tapStatus)
    beerdescription = models.CharField(max_length=400)
    def __unicode__(self):
        return self.beerdescription
    beerStyle = models.CharField(max_length=5)
    def __unicode__(self):
        return self.beerStyle
    beerabv = models.CharField(max_length=5)
    def __unicode__(self):
        return self.beerabv
    beername = models.CharField(max_length=40)
    def __unicode__(self):
        return self.beername

You probably want the BooleanField :

 class BooleanField(**options) A true/false field. The default form widget for this field is a CheckboxInput. If you need to accept null values then use NullBooleanField instead. 

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