私は次のようなmodels.pyを持っています:
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
class UserProfile(models.Model):
user = models.OneToOneField(User)
def __unicode__(self):
return self.user.username
class Project(models.Model):
user = models.ForeignKey(UserProfile)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
product = models.ForeignKey('tool.product')
module = models.ForeignKey('tool.module')
model = models.ForeignKey('tool.model')
zipcode = models.IntegerField(max_length=5)
def __unicode__(self):
return unicode(self.id)
そして私のtests.py:
from django.test import TestCase, Client
# --- import app models
from django.contrib.auth.models import User
from tool.models import Module, Model, Product
from user_profile.models import Project, UserProfile
# --- unit tests --- #
class UserProjectTests(TestCase):
fixtures = ['admin_user.json']
def setUp(self):
self.product1 = Product.objects.create(
name='bar',
)
self.module1 = Module.objects.create(
name='foo',
enable=True
)
self.model1 = Model.objects.create(
module=self.module1,
name='baz',
enable=True
)
self.user1 = User.objects.get(pk=1)
...
def test_can_create_project(self):
self.project1 = Model.objects.create(
user=self.user1,
product=self.product1,
module=self.module1,
model=self.model1,
zipcode=90210
)
self.assertEquals(self.project1.zipcode, 90210)
しかし、TypeError: 'product' is an invalid keyword argument for this function
エラーが発生します。
何が失敗しているのかわかりませんが、FKの関係と関係があると思います...
どんな助けでも大歓迎です。
編集: 完全なトレースバック:
ERROR: test_can_create_project (user_profile.tests.UserProjectTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sam/Dropbox/django-projects/unirac/user_profile/tests.py", line 52, in test_can_create_project
zipcode=90210
File "/home/sam/.envs/unirac/local/lib/python2.7/site-packages/django/db/models/manager.py", line 137, in create
return self.get_query_set().create(**kwargs)
File "/home/sam/.envs/unirac/local/lib/python2.7/site-packages/django/db/models/query.py", line 375, in create
obj = self.model(**kwargs)
File "/home/sam/.envs/unirac/local/lib/python2.7/site-packages/django/db/models/base.py", line 367, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
TypeError: 'product' is an invalid keyword argument for this function