使用字典编写数据库时“对象具有关键字参数的多个值”

PK1*_*K10 1 python django django-models

我有一个字段的Django模型:city,country_name和country_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()
Run Code Online (Sandbox Code Playgroud)

jon*_*rpe 5

您正在传递以下两个参数country_code:

m = Logger(city='city', country_name='country_name',
           country_code='country_code', **dicty)
#          ^ here                       ^ and in here
Run Code Online (Sandbox Code Playgroud)

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

m = Logger(city=dicty['city'], country_name=dicty['country_name'],
           country_code=dicty['country_code'])
Run Code Online (Sandbox Code Playgroud)