gpt4 book ai didi

Django 。此函数的关键字参数无效。多对多

转载 作者:行者123 更新时间:2023-12-04 02:59:10 27 4
gpt4 key购买 nike

我有这个错误:

'people' 是此函数的无效关键字参数

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')

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

最佳答案

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

up = UserPassage.objects.create(passage=passage)
up.people.add(user_profil)
ManyToMany关系不会保存为表中的列。阅读这里的第一个回复以获得很好的解释:

Django ManyToMany field is not created in model

@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 。此函数的关键字参数无效。多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13144051/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com