简体   繁体   中英

Option or state Field like a boolean field in a django model

I have this model - please look at the state variable.
I would like this to be a drop-down list shown on a form and one of these options get's "picked". What do I look for in documentation.

I want to allow the user to change the state givin these options. However ,I might want to add more states in the admin. and when the state changes it may send a "signal" for a method

class Transaction(models.Model):
    creator = models.SlugField()
    amount = models.DecimalField(max_digits= 10000000,decimal_places =2,null= True,default=0)
    accepted_by = models.SlugField()
    oferto_slug = models.SlugField()
    state = (
            'handshake',
            'delivered',
            'canceled'.

You won't be able to add additional state options without persisting them somewhere. As @karthikr stated, you should move these options to a separate model so you can change the options via Django admin:

class State(models.Model):
    title = models.CharField(max_length=50)

    def __unicode__(self):
        return self.title


class Transaction(models.Model):
    ...
    state = models.ForeignKey(State)

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