Django:此函数的关键字参数无效 [英] Django: invalid keyword argument for this function

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

问题描述

我想将一些数据插入到多个字段中。我收到此错误


用户是此函数的无效关键字参数


我也尝试了相关的名称...但仍然没有工作...



我的模型看起来像这样: / p>

models.py

  class Workspace(models.Model):
user = models.ManyToManyField(User,null = False,blank = False,related_name ='members')
workspace_name = models.CharField(max_length = 80,null = False,blank = False)
workspace_cat = models.CharField(max_length = 80,null = True,blank = True)

py

  db = Workspace(user = 5,workspace_name = data_to_db ['workspace_name'],workspace_cat = data_to_db ['workspace_category'] )
db.save()

有人有想法吗?非常感谢!

解决方案

你使用了一个用户的/ref / models/fields/#django.db.models.ManyToManyField> ManyToMany 字段工作空间对象的c>字段,您不能给它一个用户,这不是一个 ManyToMany 的工作原理,那将是一个 ForeignKey



基本上,使用 ForeignKey ,每个工作区都有一个用户与它相关联,直接链接 Workspace - >用户,所以创建一个工作空间并将其传递给用户您将填写 CharField



A ManyToMany 关系意味着几个用户可以与 Workspace 和几个工作区关联到一个用户。当使用 ManyToMany 时,您将创建您的工作区然后添加一些 User s。



要添加到 ManyToMany 请执行以下操作:

  my_user = User.objects.get(pk = 5)
my_workspace = Workspace(workspace_name = data_to_db ['workspace_name'],workspace_cat = data_to_db ['workspace_category'])
my_workspace.save()#首先对于DOM(Jurudocs编辑)是必需的。
my_workspace.users.add(my_user )

应重命名用户字段到用户 ,使关系名称更清晰。


I want to insert some data into a many to many field. I 'm getting this Error

user is an invalid keyword argument for this function

i also tried it with the relatedName...but still is not working...

My model looks like this:

models.py

class Workspace(models.Model):
    user = models.ManyToManyField(User,null=False, blank=False, related_name='members')
    workspace_name = models.CharField(max_length=80, null=False, blank=False)
    workspace_cat =models.CharField(max_length=80, null=True, blank=True)

views.py

db= Workspace(user=5, workspace_name=data_to_db['workspace_name'],workspace_cat=data_to_db['workspace_category'])
db.save()

Does somebody has an idea? Thanks a lot!

解决方案

You used a ManyToMany field for the user field of your Workspace object, you can't give it one user, that's not how a ManyToMany works, that would be a ForeignKey.

Basically, using a ForeignKey, each workspace has one User associated to it, there's a direct link Workspace -> User, so it makes sense to create a Workspace and pass it an User, like you would be filling in a CharField.

A ManyToMany relationship means that several users can be associated to a Workspace and several Workspaces to one User. When using a ManyToMany, you would create your Workspace and then add some Users to it.

To add to a ManyToMany relationship, do the following:

my_user = User.objects.get(pk = 5)
my_workspace = Workspace(workspace_name=data_to_db['workspace_name'],workspace_cat=data_to_db['workspace_category'])
my_workspace.save() # committing to the DB first is necessary for M2M (Jurudocs edit)
my_workspace.users.add(my_user)

You should rename the user field to users to make the relationship name clearer.

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

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