当使用字典编写数据库时,“对象获得了关键字参数的多个值”

时间:2014-01-01 07:58:04

标签: python django django-models

我的django模型包含字段:citycountry_namecountry_code

我有一个字典,它有超过8个键值对;我只想使用3并写入数据库。

但我收到错误

ModelBase object got multiple values for keyword argument 'country_code'

我的代码:

dicty = {
    'city': u'Mountain View', 'continent_code': u'NA', 'region': u'CA',
    'charset': 0, 'area_code': 650, 'longitude': -122.05740356445312,
    'country_code3': u'USA', 'latitude': 37.4192008972168, 'postal_code': u'94043',
    'dma_code': 807, 'country_code': u'US', 'country_name': u'United States'
}

m = Logger(city='city',country_name='country_name',country_code='country_code',**dicty)
m.save()

1 个答案:

答案 0 :(得分:5)

您正在为country_code传递两个参数:

m = Logger(city='city', country_name='country_name',
           country_code='country_code', **dicty)
#          ^ here                       ^ and in here

因此,您同时获得country_code = "country_code" country_code = u"US"。如果您只想从字典中传递三个值,请执行以下操作:

m = Logger(city=dicty['city'], country_name=dicty['country_name'],
           country_code=dicty['country_code'])