On Thursday, 9 February 2012 11:29:52 UTC, anand jeyahar wrote:
>
> 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??
>
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.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/oPrHxzu4H6QJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.