ModelForms中的复选框

时间:2015-01-11 16:45:55

标签: python django

我有模型人物和模型事件,我想创建一个表单,其中我有一个人员列表,其中包含一个事件的复选框。 我做错了什么?

models.py:

class Applications(models.Model):
    person = models.ForeignKey(Persons, related_name='ApplPer')
    events = models.ForeignKey(Events, related_name='ApplEve')
    add_date = models.DateTimeField('Date', auto_now_add=True)

views.py:

def events_list(request, slug, event_slug):
        event = models.Events.objects.get(slug=event_slug)
        team_id = models.Druzyny.objects.filter(slug=slug)
        person_list = models.Osoby.objects.filter(dru=team_id).filter(aktywny=1).order_by('nazwisko')

        if request.method == 'POST':
            form = ApplicationForm(request.POST, team=1)
            if form.is_valid():
                form.save()
                return render_to_response('strona/events_list.html',
                                          {'slug': slug, 'event_slug': event_slug,
                                           'event': event,'person_list': person_list,
                                           'form': form },
                                          context_instance=RequestContext(request), )
        else:
            form = ApplicationForm(team=1)
        return render_to_response('strona/events_list.html',
                                  {'slug': slug, 'event_slug': event_slug,
                                   'event': event,'person_list': person_list,
                                   'form': form },
                                  context_instance=RequestContext(request), )

class ApplicationForm(forms.ModelForm):

    class Meta:
        model = models.Applications
        fields = ('imp','oso')

    def __init__(self, *args, **kwargs):
        my_team  = kwargs.pop('team')

        super(ApplicationForm, self).__init__(*args, **kwargs)

        self.fields['oso'].widget = CheckboxSelectMultiple()
        self.fields['oso'].queryset = models.Osoby.objects.filter(dru=my_team).filter(aktywny=1).order_by('nazwisko')

我改变观点,现在我有:

int() argument must be a string or a number, not 'list'

我想要这个结果:

Person1 [Checkbox]
Person2 [Checkbox]
Person3 [Checkbox]

1 个答案:

答案 0 :(得分:0)

您致电fromdata.save()。但formdata不是您的表单数据。没什么。

要在表单中保存表单数据,请在表单上调用save:

form.save()

请参阅文档:https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#the-save-method

更新

我没有看到模型的导入,但最好只导入所需的模型,而不是完整的模块。

 import models

应该是:

from .models import Applications

也不是说拥有单一的模型名称会更好:'应用程序'。