Dan Martino
Dan Martino

Reputation: 347

ValueError when querying sqlite db in Django

class Event(models.Model):
    student = models.ForeignKey(User)
    startTime = models.DateTimeField('Start Time')
    endTime = models.DateTimeField('End Time')

def __str__(self):              
    return str(self.student)

Above is my model that sets up the Event table. I have it set up so the foreign key 'student' is pulled from the admin users on the Django site.

When I run Event.objects.all():

[<Event: test.student1>]

Which is good.

When I run Event.objects.get(student='test.student1'):

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 325, in get
clone = self.filter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 697, 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 1304, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1332, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1194, in build_filter
lookups, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1740, in get_lookup_constraint
lookup_class(target.get_col(alias, source), val), AND)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 96, in __init__
self.rhs = self.get_prep_lookup()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/lookups.py", line 134, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 727, 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 985, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'test.student1'

I feel the issue has to do with student being a foreign keys from the Users table (I am importing from django.contrib.auth.models import User). I cannot for the life of me figure out what I'm doing wrong.

Upvotes: 0

Views: 131

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 47846

When looking up Event via student, you have to pass the actual student instance.

Event.objects.get(student=student_instance)

What is happening in your case:

For model Student(derived from Django's User model), __str__() method returns the username. So, when you do str(self.student) in your __str__() method of the Event model , you get the username of the student instance. You are assuming that test.student1 is the student instance but infact its the username of the student instance.

Change your query to access via student's username i.e student__username and it should work.

Event.objects.get(student__username='test.student1')

Upvotes: 0

page
page

Reputation: 21

The student attribute is a foreign key (an integer), you need to search the username of the Student instead.

Event.objects.get(student__username='test.student1')

Upvotes: 2

Related Questions