使用Django中的中间模型,使ModelForm与ManyToMany关系工作的步骤是什么? [英] What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django?

查看:912
本文介绍了使用Django中的中间模型,使ModelForm与ManyToMany关系工作的步骤是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我有一个客户端 Groupe 模型。

  • em>可以是多个的一部分。

  • 客户端是组的一部分,可以随时使用其组的免费租用费率但只有一次。这就是中介模式( ClientGroupe )来自于额外的数据。

  • I have a Client and Groupe Model.
  • A Client can be part of multiple groups.
  • Clients that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (ClientGroupe) comes in with that extra data.

现在,当我试图保存m2m数据,它只是死了,说我应该使用ClientGroupe Manager ...所以没有什么?

For now, when I try to save the m2m data, it just dies and says I should use the ClientGroupe Manager...so what's missing?

class Groupe(models.Model):
    nom = models.CharField(max_length=1500, blank=True)

class Client(models.Model):
    nom = models.CharField(max_length=450, blank=True)
    prenom = models.CharField(max_length=450, blank=True)
    groupes = models.ManyToManyField(Groupe, null = True, blank = True, through='ClientGroupe')

class ClientGroupe(models.Model):
    client = models.ForeignKey(Client)
    groupe = models.ForeignKey(Groupe)
    dt = models.DateField(null=True, blank=True) # the date the client is using its group's free rental rate    

    class Meta:
        db_table = u'clients_groupes'



这里是我的观点:



and here's my view:

def modifier(request, id):
    client = Client.objects.get(id=id)    
    form = ClientForm(instance = client)

    dict = {
        "form": form
        , "instance" : client
    }

    if request.method == "POST":
        form = ClientForm(request.POST, instance = client)

        if form.is_valid():
            client_mod = form.save()

            id = client_mod.id
            return HttpResponseRedirect(
                "/client/%(id)s/?err=success" % {"id" : id}
            )
        else:
            return HttpResponseRedirect(
                "/client/%(id)s/?err=warning" % {"id" : id}
            )

    return render_to_response(
        "client/modifier.html"
        , dict
        , context_instance=RequestContext(request)
    )

编辑

这里是ClientForm代码:

and here's the ClientForm code:

class ClientForm(ModelForm):
    class Meta:
        model = Client

编辑#2
这里是错误消息:

EDIT #2: here's the error message:

AttributeError at /client/445/

Cannot set values on a ManyToManyField which specifies an intermediary model. Use ClientGroupe's Manager instead.

Request Method:     POST
Request URL:    http://localhost/client/445/
Exception Type:     AttributeError
Exception Value:    Cannot set values on a ManyToManyField which specifies an intermediary model.  Use ClientGroupe's Manager instead.

Exception Location:     C:\Python25\lib\site-packages\django\db\models\fields\related.py  in __set__, line 574
Python Executable:  C:\xampp\apache\bin\apache.exe
Python Version:     2.5.2


推荐答案

…
if form.is_valid():
    client_mod = form.save(commit=False)
    client_mod.save()
    for groupe in form.cleaned_data.get('groupes'):
        clientgroupe = ClientGroupe(client=client_mod, groupe=groupe)
        clientgroupe.save()
    …

这篇关于使用Django中的中间模型,使ModelForm与ManyToMany关系工作的步骤是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆