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.
Aucun commentaire:
Enregistrer un commentaire