I have a models.py like so:
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)
And my 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)
But I get a TypeError: 'product' is an invalid keyword argument for this function error.
I’m not sure what is failing but I’m guessing something to do with the FK relationships…
Any help would be much appreciated.
EDIT:
Full Traceback:
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
Model.objects.create(… should likely beProject.objects.create(At:
On a sidenote, naming your
Model,Model, asfrom tool.models import Module, Model, Productseems to imply, is a bad idea.On another sidenote, the traceback usually provides very useful information as to where the error occured. Here, you can see that the traceback indicates the error occured in:
Before jumping to library code, which you can reasonably expect to not be buggy.