开发者

django forms exclude values from choicefield

I have the following model

class ActionConfirm(models.Model):
    CONFIRM_METHOD =  (
        (u'ce', u'Certificate'),
        (u'tf', u'Trainee Feedback'),
        (u'ms', u'Multi Source Feedb开发者_JS百科ack'),
        (u'rp', u'Reflection upon Practice'),
        (u'ot', u'Other - Please add/describe')
    )

    confirm_method = models.CharField(max_length=2, choices=CONFIRM_METHOD)
    user = User

and the following form

class ActionConfirmForm(forms.ModelForm):
    class Meta:
        model = ActionConfirm

and I know that I can get their current choices by doing

selected = ActionConfirm.objects.filter(user=user)

So how do I exclude values from the the confirm_method field which they have already selected?

If it was from a db I know I could do choices = ActionConfirm.objects.exclude(choice__in = selected) but I don't know how to do it when it is from a tuple of tuples.


You don't show the relationship between ActionConfirm and ConfirmChoices. Why is confirm_method a CharField rather than a ForeignKey?

However, if you can get the selected choices, you can exclude them in the __init__ of the form:

def __init__(self, *args, **kwargs):
    super(ActionConfirmForm, self).__init__(*args, **kwargs)
    selected_choices = whatever
    self.fields['confirm_method'].choices = [(k, v) for k, v in CONFIRM_METHOD
                                             if k not in selected_choices]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜