Python 3.x Django-类型错误;参数有多个值
我有一个对象函数来检查数据库中是否已经有一个位置。在我添加检查功能之前,一切都正常 models.pyPython 3.x Django-类型错误;参数有多个值,python-3.x,django-models,django-2.0,Python 3.x,Django Models,Django 2.0,我有一个对象函数来检查数据库中是否已经有一个位置。在我添加检查功能之前,一切都正常 models.py class LocationManager(models.Manager): def exist(city, state): if self.queryset.get(city=city, state=state): return True return False def create_new(self, ip_add
class LocationManager(models.Manager):
def exist(city, state):
if self.queryset.get(city=city, state=state):
return True
return False
def create_new(self, ip_address, city_data):
location = self.model()
if ip_address is not None:
if city_data:
city = city_data['city']
state = city_data['region']
if not self.exist(city=city, state=state):
location.city_data = city_data
location.city = city
location.state = state
location.country = city_data['country_name']
location.latitude = city_data['latitude']
location.longitude = city_data['longitude']
location.save()
return location
else:
return Location.objects.get(city=city, state=state)
class Location(models.Model):
city_data = models.TextField(null=True, blank=True)
city = models.CharField(max_length=120, null=True, blank=True)
state = models.CharField(max_length=2, null=True, blank=True)
country = models.CharField(max_length=20, null=True, blank=True)
latitude = models.FloatField()
longitude = models.FloatField()
objects = LocationManager()
def __str__(self):
return '{}, {}'.format(self.city, self.state)
我得到一个错误:
TypeError at /login/
exist() got multiple values for argument 'city'
/新建中的home/slim/Desktop/django/locations/models.py
if not self.exist_(city=city, state=state):
本地变量:
city 'Mountain View'
city_data {'city': 'Mountain View',
'country_code': 'US',
'country_name': 'United States',
'dma_code': 807,
'latitude': 37.419200000000004,
'longitude': -122.0574,
'postal_code': '94043',
'region': 'CA',
'time_zone': 'America/Los_Angeles'}
ip_address '172.217.4.46'
location <Location: None, None>
self <locations.models.LocationManager object at 0x7f40b3ec6be0>
state 'CA'
城市“山景”
城市数据{'city':'Mountain View',
“国家代码”:“美国”,
“国家名称”:“美国”,
“dma_代码”:807,
“纬度”:37.41920000000004,
“经度”:-122.0574,
“邮政编码”:“94043”,
'地区':'CA',
“时区”:“美国/洛杉矶”
ip_地址“172.217.4.46”
地方
自己
国家“CA”
def存在(城市、州)
您在exist
的定义中缺少self
。这将导致实例作为city
以及显式关键字参数city=city
传递
简化示例:
class Foo:
def bar(a):
print(a)
f = Foo()
f.bar()
# <__main__.Foo object at 0x0000000003698DA0>
f.bar(a='a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bar() got multiple values for argument 'a'
Foo类:
def巴(a):
印刷品(a)
f=Foo()
f、 bar()
#
f、 bar(a='a')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:bar()为参数“a”获取了多个值
def存在(城市、州)
您在exist
的定义中缺少self
。这将导致实例作为city
以及显式关键字参数city=city
传递
简化示例:
class Foo:
def bar(a):
print(a)
f = Foo()
f.bar()
# <__main__.Foo object at 0x0000000003698DA0>
f.bar(a='a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bar() got multiple values for argument 'a'
Foo类:
def巴(a):
印刷品(a)
f=Foo()
f、 bar()
#
f、 bar(a='a')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:bar()为参数“a”获取了多个值
您忘记了将self
作为第一个参数
def exist(self, city, state):
# ^^^^
您忘记了将
self
作为第一个参数
def exist(self, city, state):
# ^^^^