在处理多个到多个字段时,我的views.py给了我错误

时间:2012-03-25 20:25:10

标签: django django-forms

我已将代码与堆栈跟踪粘贴在.. https://gist.github.com/2199510

my models.py

class Roles(models.Model):
    role = models.CharField(max_length=32)

    def __unicode__(self):
        return self.role

class Player(models.Model):
    first_name = models.CharField(max_length=32)
    last_name = models.CharField(max_length=32)
    #name = models.CharField(max_length=32)
    team = models.ForeignKey(Team)
    role = models.ManyToManyField(Roles)
    preffered_position = models.IntegerField(max_length=3,choices=zip(range(1,12),range(1,12)) , default=1)
    status = models.BooleanField(default=True)

    #career statistics
    best_batting = models.IntegerField(max_length=6, default=0)
    best_bowling = models.CharField(max_length=6, default=0)


    #total batting statistics
    score = models.IntegerField(max_length=6, default=0)
    balls = models.IntegerField(max_length=6, default=0)

    #total bowling statistics
    overs = models.IntegerField(max_length=6, default=0)
    wickets = models.IntegerField(max_length=6, default=0)

    #total fielding statistics
    catches = models.IntegerField(max_length=6, default=0)

    def __unicode__(self):
        return "%s %s" % (self.first_name, self.last_name)

my forms.py

class PlayerForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()
    role = forms.ModelMultipleChoiceField(
        Roles.objects.all(),
        widget=CheckboxSelectMultiple
    )

my views.py

def add_player(request, team_id):
    template = get_template('cricket/addplayer.html')
    loggedinuser = request.user
    userteam = Team.objects.get(owner=loggedinuser)
    currentteam = Team.objects.get(id=team_id)
    #now on to permissions .. if userteam = currentteam, the display form, otherwise display error "Permission denied"
    if userteam == currentteam:
        if request.method == 'POST':
            form = PlayerForm(request.POST)
            if form.is_valid():
                Player.objects.create(
                    first_name = form.cleaned_data['first_name'],
                    last_name = form.cleaned_data['last_name'],
                    team = Team.objects.get(id=team_id),
                    role = form.cleaned_data['role'],
                )

                return HttpResponseRedirect('/team/%s/' % team_id)
        else:
            form = PlayerForm

        page_vars = Context({
            'form': form,
            'loggedinuser': loggedinuser,
            'team': userteam,
            })

        crsfcontext = RequestContext(request, page_vars)

        output = template.render(crsfcontext)
        return HttpResponse(output)
    else:
        error = "Permission Denied: You cannot add player to " + str(currentteam.name)
        page_vars = Context({
            'loggedinuser': loggedinuser,
            'error': error,
        })

        crsfcontext = RequestContext(request, page_vars)

        output = template.render(crsfcontext)
        return HttpResponse(output)

如果有人可以告诉我哪里以及我做错了什么,那将会有很大的帮助。

//优素福

1 个答案:

答案 0 :(得分:0)

如错误消息所述,您无法在模型实例化调用中将role(或任何ManyToMany字段)作为关键字传递。您需要先实例化并保存模型,然后使用player.roles.add(my_role)添加一个或多个角色。