简体   繁体   English

选项或状态字段,如Django模型中的布尔字段

[英]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. 如果不将其他state选项保留在某个位置,则无法添加它们。 As @karthikr stated, you should move these options to a separate model so you can change the options via Django admin: 如@karthikr所述,您应将这些选项移至单独的模型,以便可以通过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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM