Mint
Mint

Reputation: 1059

Django - TypeError ; got multiple values for argument

i have a object function to check if a location is already in the database. everything was working until i added the check function.

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_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)

I'm getting error:

TypeError at /login/
exist() got multiple values for argument 'city'

/home/slim/Desktop/django/locations/models.py in create_new

if not self.exist_(city=city, state=state):

Local vars:

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'

Upvotes: 1

Views: 3194

Answers (2)

awesoon
awesoon

Reputation: 33651

You forgot self as the first parameter.

def exist(self, city, state): 
    #     ^^^^

Upvotes: 4

DeepSpace
DeepSpace

Reputation: 81594

def exist(city, state)

You are missing self in exist's definition. This causes the instance to be passed as city along with the explicit keyword argument city=city.

Simplified example:

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' 

Upvotes: 1

Related Questions