django error

14 views
Skip to first unread message

anand jeyahar

unread,
Feb 9, 2012, 6:29:52 AM2/9/12
to Django users
Hi,
Am rather new to django and this error makes no sense to me..


Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/
base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/
base.py", line 522, in save_base
manager.using(using).filter(pk=pk_val).exists())):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/
query.py", line 550, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/
query.py", line 568, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/
query.py", line 1194, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/
query.py", line 1129, in add_filter
connector)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/
where.py", line 67, in add
value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/
where.py", line 316, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/
__init__.py", line 292, in get_prep_lookup
return self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/
__init__.py", line 479, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: '192.168.8.123'

Rather what i don't understand is why django is trying to int casting
the string... find attached the models.py file and the script am
running in python manage.py shell.

models.py:

#!/usr/bin/env python
import datetime
from django.db import models

class Lock(models.Model):

# id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100,unique=True)
#value = models.IntegerField(null=True)
# No point.. if the record exists, there's a lock, else there
isn't.
info = models.CharField(max_length=1000)
timestamp = models.DateTimeField(auto_now=True)

@classmethod
def clean(self, name, age=0):
# Get a datetime that is offset by 'age' seconds
dt = datetime.datetime.now() - datetime.timedelta(seconds=age)

# Query and delete
old = Lock.objects.filter(name=name , timestamp__lte=dt)
old.delete()


Here's the code that triggers the error.

from locker.models import *

name = "192.168.8.123"
info = "Migrating VIP %s at %s "%("234.34.32.342","12.34.43.22")
lock = Lock(name,info)
lock.save()


now from what i understand django tries to search the table with the
primary key as the name field. But django documentation says it by
default creates an automatic primary key field 'id'. My question is
why does it expect it to be explicitly passed??

Daniel Roseman

unread,
Feb 9, 2012, 8:19:08 AM2/9/12
to django...@googlegroups.com
There's no searching going on here. You create a lock instance, passing two positional arguments, which Django interprets as the first fields, ID and name. 

That's why you should never use positional arguments when instantiating - always do it via keyword arguments:
lock = Lock(name=name, info=info)

Note that as I say, you're not actually checking if there's a lock with that name already. You probably want to actually do that.
--
DR.

anand jeyahar

unread,
Feb 9, 2012, 8:41:31 AM2/9/12
to django...@googlegroups.com

Hi




There's no searching going on here. You create a lock instance, passing two positional arguments, which Django interprets as the first fields, ID and name. 

That's why you should never use positional arguments when instantiating - always do it via keyword arguments:
lock = Lock(name=name, info=info)

Thanks a lot Daniel.. i feel dumb now..
 
Note that as I say, you're not actually checking if there's a lock with that name already. You probably want to actually do that.

Hmm.. i was just happy to leave it to the db to raise an error.  since i use the unique key, that should work..  
I agree should put in a check for the type of exception and display the right message though... 

Daniel Roseman

unread,
Feb 9, 2012, 8:56:40 AM2/9/12
to django...@googlegroups.com
You can use `get_or_create` to do this check for you:
--
DR. 
Reply all
Reply to author
Forward
0 new messages