本文介绍了Django的。此函数的关键字参数无效。多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个错误:

人是此功能的无效关键字参数

class Passage(models.Model):
    name= models.CharField(max_length = 255)
    who = models.ForeignKey(UserProfil)

class UserPassage(models.Model):
    passage = models.ForeignKey(Passage)
    people = models.ManyToManyField(UserProfil, null=True)

class UserProfil(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=50)

我尝试:

def join(request):
    user = request.user
    user_profil = UserProfil.objects.get(user=user)
    passage = Passage.objects.get(id=2)
    #line with error
    up = UserPassage.objects.create(people= user_profil, passage=passage)
    return render_to_response('thanks.html')

如何正确地做到这一点?谢谢!

How does one do this correctly? Thanks!

推荐答案

您需要保存/创建对象,才能添加 ManyToMany 关系:

You need to save/create the object before you can add ManyToMany relationships:

up = UserPassage.objects.create(passage=passage)
up.people.add(user_profil)

ManyToMany 关系不保存为表格中的列。请阅读第一个回复,以获得良好的解释:

ManyToMany relationships aren't saved as columns in your table. Read the first reply here for good explanation:

这篇关于Django的。此函数的关键字参数无效。多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 12:16