我如何将模型加载到multipleChoiceField

时间:2013-05-21 01:28:34

标签: django django-models django-forms django-templates django-views

请你一直试图在表格中显示表格,但我无法做到。 请克隆并创建数据库并在github上运行该项目。 要粘贴的代码太多了。

github:https://github.com/mattisbmx/django-multipleChoiceField

型号:

from django.db import models
from multiselect.fields import ManyToManyField

class Choice(models.Model):
    choice = models.CharField(max_length=15)

    def __unicode__(self):
        return self.choice

class SampleModel(models.Model):
    name = models.CharField(max_length=15)
    funciones = ManyToManyField(Choice)
    passwd = models.TextField()
    def __unicode__(self):
        return unicode(self.pk)

查看:

def index(request):
    data = {'form': forms.SelectForm()}

    return render_to_response("multiselect/index.html", data,
                          context_instance=RequestContext(request

形式:

class SelectForm(forms.Form):
    data = (('1', 'One'), ('2', 'Two'), ('3', 'Three'), ('4', 'Four')) #<--I think here I load the data model
    choices = MultipleChoiceField(choices=data)

由于

1 个答案:

答案 0 :(得分:3)

您可以使用ModelMultipleChoiceField

class SelectForm(forms.Form):
    choices = forms.ModelMultipleChoiceField(queryset=Choice.objects.all()) #Replace the queryset with the queryset of your choice.

如果您想更改默认小部件

您可以使用widget=CheckboxSelectMultiple()或任何您想要的人。