Django的。此函数的关键字参数无效。多对多 [英] Django. Invalid keyword argument for this function. ManyToMany
问题描述
我有这个错误:
人是此功能的无效关键字参数
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:
@DanielRoseman:因为ManyToMany isn'一个字段,至少不存在一个数据库列。这是与链接表的关系。你会发现一个名为myapp_teacher_subjects的表已经被创建,外键有老师和主题。
@DanielRoseman: Because a ManyToMany isn't a field, at least not one that exists as a database column. It's a relationship with a linking table. You'll find that a table named myapp_teacher_subjects has been created, with foreign keys to both teacher and subjects.
这篇关于Django的。此函数的关键字参数无效。多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!