0

请您一直在尝试以表格形式显示表格,但我无法做到。请克隆并创建数据库并在 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)

谢谢

4

1 回答 1

4

您可以使用ModelMultipleChoiceField

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

如果您想更改默认小部件

您可以使用widget=CheckboxSelectMultiple()或随心所欲。

于 2013-05-21T02:06:40.613 回答