开发者

Multiple textbox and checkbox based on values in database (Django)

I have a model that looks something like this

ID, name, isValue, Value

What I want the logic to do is like this in layman terms :

if isValue is false, present the user with a textbox so that he can fill it in,

else , present the user with a checkbox

In the database, all of these columns are filled in except Value column. I don't really need any code but I appreciate if anybody can show me how to move on from here.

Just to start off, I'm not sure wheth开发者_Go百科er it is possible to pass the (isValue=false) objects to the checkbox widget, and pass the (isValue=True) objects to the textbox widget, and then display it on my template, just not sure how. I did something like this to separate the objects. So what I wanted is to display all 13 entries(for example, there are 13 entries in that table) with its name and checkbox/textbox based on isValue.

checkboxx = []
textboxx  = []

items = Items.objects.all()
for i in items:
       if i.isValue == False:
          checkboxx.append(i)
       else
          textboxx.append(i)


Use a custom form. Then in it's __init__ method assign a Textarea or CheckboxInput widget as the field's widget based on whatever evaluation you like.

class MyModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)

        if self.instance.isValue:
            self.fields['myfield'].widget = forms.Textarea()
        else:
            self.fields['myfield'].widget = forms.CheckboxInput()

Above is a simplified example. You'll have to modify to fit your logic, but the basic principle applies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜