Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:ModelBase对象为关键字参数获取了多个值_Python_Django_Unit Testing - Fatal编程技术网

Python TypeError:ModelBase对象为关键字参数获取了多个值

Python TypeError:ModelBase对象为关键字参数获取了多个值,python,django,unit-testing,Python,Django,Unit Testing,我得到了一份工作 TypeError:ModelBase对象为关键字参数获取了多个值 “日期” 当我试图创建一个“城市”时,在我的测试框架上 这是我的回溯: ERROR: test_create_city (app.tests.AppManagementTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File

我得到了一份工作

TypeError:ModelBase对象为关键字参数获取了多个值 “日期”

当我试图创建一个“城市”时,在我的测试框架上


这是我的回溯:

ERROR: test_create_city (app.tests.AppManagementTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "..tests.py", line 158, in test_create_city
    city_obj = City(user = self.user, category = c, date = datetime.datetime.now(), **data)
TypeError: ModelBase object got multiple values for keyword argument 'date'
我的代码是:

def test_create_city(self):
c = Category(name=self.categories[0]['name'])
c.save()
data = {'vehiclesound': '/assets/sounds/vehicle.ogg', 'vehicleshadow': '', 'maxspeed': 160.0, 'suspensionrestlength': 0.5, 'category': 12L, 'leftpub': '8294092164', 'wheelmodelzscale': 0.7, 'camheight': 2.1, 'speedminturn': 50.0, 'suspensiondeltatime': 0.25, 'crashsound': '/assets/sounds/crash.ogg', 'decel': 40.0, 'camtilt': 90.0, 'turnspeedmin': 20.0, 'path': 'just_testing_path', 'frontrightwheel': '', 'limitlinealpha': '01', 'open': 1, 'id': 35L, 'limitheight': 700L, 'modelzscale': 0.7, 'model_complete': 'http://youbeq.org/models/get.php?file=default_app/model.dae', 'rearleftwheel': '', 'user_id': 1L, 'wheelmodelyscale': 0.7, 'allwheels': 'http://sketchup.google.com/3dwarehouse/download?mid=4bc3b6056f5cd97eb5d8f6f0e9fb0ac&rtyp=ks&fn=taxi_4tires&ctyp=other&prevstart=0&ts=1343322996000', 'limitlinecolor': 'FFFFFF', 'limitcolor': '00ffff', 'gravity': 70.0, 'modelxscale': 0.7, 'limitlinewidth': 2L, 'kms': 9007.25, 'axisdistance': 2.5, 'minaccelstep': 5.0, 'wheelsdistance': 1.0, 'limitalpha': '70', 'turnspeedmax': 60.0, 'traildistance': 10.0, 'suspensionstiffness': 0.5, 'vehicletype': 'car', 'description': 'The City that never sleeps', 'wheelsheight': 0.37, 'vehiclesoundtime': 150.0, 'rollclamp': 50.0, 'accel': 5.0, 'backgroundsoundtime': 150.0, 'wheelmodelxscale': 0.7, 'rightpub': '5607834847', 'key': 'just_testing_key', 'accelstep': 25.0, 'date': None, 'world': 'earth', 'mapiconurl': '', 'vehicleagility': 0.0005, 'footer_large': '/assets/img/new_york.png', 'modelheight': 0.0, 'frontleftwheel': '', 'speedmaxturn': 5.0, 'name': 'New York', 'footer': '/taxi/assets/img/taxi_smarturbia_image_new_york.png', 'suspensiondamping': -0.15, 'rearrightwheel': '', 'crashsoundtime': 150.0, 'vehiclefastsoundtime': 150.0, 'maxrevspeed': 15.0, 'mass': 3000.0, 'backgroundsound': '/assets/sounds/background.ogg', 'published': 1, 'modelyscale': 0.7, 'model': 'http://sketchup.google.com/3dwarehouse/download?mid=128bf1862f1eb56db5d8f6f0e9fb0ac&rtyp=ks&fn=taxi_new_york_chassi&ctyp=other&prevstart=12&ts=1343297355000', 'vehiclefastsound': '', 'rollspring': 0.5, 'steerroll': -1.0}
        print self.user
        try :
            city_obj = City.objects.get(key=self.categories[0]['name'])
            print ("city_already_exists")
        except City.DoesNotExist:
            print ("debug")
            city_obj = City(user = self.user, category = c, date = datetime.datetime.now(), **data)
            city_obj.save()

在Python中,您可以传递散列并使用
**
来代替关键字参数。考虑这个例子:

>>> def fun(x, y):
...   pass
... 
>>> hash = {'x': 1, 'y': 2}
>>> fun(**hash) # OK
>>> fun(x=3, **hash) # x defined both explicitly and in hash
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fun() got multiple values for keyword argument 'x'
>>定义乐趣(x,y):
...   通过
... 
>>>哈希={'x':1,'y':2}
>>>乐趣(**散列)#好的
>>>fun(x=3,**散列)#x在散列中和显式定义
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:fun()为关键字参数“x”获取了多个值
在您的例子中,
City
构造函数使用
date
两次:在
数据中有
'date':无
,并使用
date=datetime.datetime.now()显式传递给
City


要修复此代码,您应该从
数据
散列中删除
日期
,这样它就不会与显式参数冲突。

在Python中,您可以传递散列并使用
**
来代替关键字参数。考虑这个例子:

>>> def fun(x, y):
...   pass
... 
>>> hash = {'x': 1, 'y': 2}
>>> fun(**hash) # OK
>>> fun(x=3, **hash) # x defined both explicitly and in hash
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fun() got multiple values for keyword argument 'x'
>>定义乐趣(x,y):
...   通过
... 
>>>哈希={'x':1,'y':2}
>>>乐趣(**散列)#好的
>>>fun(x=3,**散列)#x在散列中和显式定义
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:fun()为关键字参数“x”获取了多个值
在您的例子中,
City
构造函数使用
date
两次:在
数据中有
'date':无
,并使用
date=datetime.datetime.now()显式传递给
City


要修复此代码,您应该从
数据
散列中删除
日期
,这样它就不会与显式参数冲突。

您的
数据
字典包含
'date':无
,因此,正如错误所述,由于您还显式地在关键字参数中传递了日期,因此您要传递两次日期

您可能希望执行以下操作:

new_data = {'user': self.user, 'category': c, 'date': datetime.datetime.now()}
data.update(new_data)
city_obj = City(**data)

(注意这会修改
数据
字典,如果您不想这样,那么应该先复制它。)

您的
数据
字典包含
'date':无
,因此,正如错误所示,由于您还显式地在关键字参数中传递了日期,因此您要传入两次日期

您可能希望执行以下操作:

new_data = {'user': self.user, 'category': c, 'date': datetime.datetime.now()}
data.update(new_data)
city_obj = City(**data)

(注意这会修改
数据
字典,如果你不想这样,那么你应该先复制它。)

谢谢,就是这样,尽管我仍然不确定第一个日期是从哪里来的,因为我是从另一个刚刚工作的代码复制粘贴的。。不管怎么说,它现在起作用了;)我现在明白了,我发现问题与你所说的无关,而是与“footer_large”中的一个关键错误有关。这让我的巨蟒疯了。我现在可以留下我上面的代码,它可以工作了。但是我仍然会把你的答案标记为正确的答案,因为它适用于这个特定的案例。谢谢,就是这样,尽管我仍然不确定第一次的日期是从哪里来的,因为我是从另一个代码中复制粘贴的。。不管怎么说,它现在起作用了;)我现在明白了,我发现问题与你所说的无关,而是与“footer_large”中的一个关键错误有关。这让我的巨蟒疯了。我现在可以留下我上面的代码,它可以工作了。但我仍然会将您的答案标记为正确答案,因为它适用于此特定情况。谢谢。尽管我不得不接受另一个答案,因为它更容易理解,并且有一个代码解决方案。谢谢,谢谢。谢谢。虽然我不得不接受另一个答案,因为它更容易理解,并且有一个代码解决方案。谢谢你