I have a
class Posts(models.Model):
created_by = models.ForeignKey(User)
post = models.CharField(max_length=150)
active = models.NullBooleanField()
def __unicode__(self):
return self.post
and now i want to filter my class Posts by created_by=testuser. For that i do
p = Posts.objects.filter(created_by=poster, active=True)
whereby var(poster) comes from the requested url(r'^(?P<poster>\w+)/$', views.Posts),
If i execute it by going to my-domain.tld/testuser i get a ValueError: invalid literal for int() with base 10: 'testuser'
If i try to filter in manage.py shell i get the error NameError: name 'testuser' is not defined if i try to filter by the command p = Posts.objects.filter(created_by=testuser).
How is it done right to filter my field created_by that content is models.ForeignKey(User). Is there a solution anyway or should i create additional a CharField with creator (poster) as content?
p = Posts.objects.filter(created_by__username=poster, active=True)poster is just an name (str), you can span relationships and query by user.username using
__or you could fetch a user object first by poster
User model documentation can be found on site: https://docs.djangoproject.com/en/dev/topics/auth/#users