我有一个这样的模型:
from django.db import models
from django.contrib.auth.models import User
class Skill(models.Model):
title = models.CharField(max_length=255)
def __unicode__(self):
return self.title
class UserSkills(models.Model):
user = models.ForeignKey(User)
skill = models.ForeignKey(Skill)
def __unicode__(self):
return '%s | %s' % (self.user, self.skill)
当我尝试这样做时:
>>> u = User.objects.get(username='myuser')
>>> s, created = Skill.objects.get_or_create(title='mysql')
>>> type(u)
<class 'django.contrib.auth.models.User'>
>>> type(s)
<class 'django_myapp.models.Skill'>
>>> userskill, created = UserSkills.objects.get_or_create(skill=s, user=u)
TypeError: 'skill' is an invalid keyword argument for this function
我在这里做错了什么?
顺便说一句,我觉得我的UserSkills
模型可能是多余的——它应该只是模型的一个字段ManyToMany
吗?Skill
User
编辑
追溯:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/sam/.envs/rs-open-auth/local/lib/python2.7/site-packages/django/db/models/manager.py", line 149, in create
return self.get_query_set().create(**kwargs)
File "/home/sam/.envs/rs-open-auth/local/lib/python2.7/site-packages/django/db/models/query.py", line 414, in create
obj = self.model(**kwargs)
File "/home/sam/.envs/rs-open-auth/local/lib/python2.7/site-packages/django/db/models/base.py", line 415, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])