Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:14:20+00:00 2026-06-16T01:14:20+00:00

I have a class Posts(models.Model): created_by = models.ForeignKey(User) post = models.CharField(max_length=150) active = models.NullBooleanField()

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:14:22+00:00Added an answer on June 16, 2026 at 1:14 am

    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 = Users.objects.get(username=poster)
    p = Posts.objects.filter(created_by=user, active=True)
    

    User model documentation can be found on site: https://docs.djangoproject.com/en/dev/topics/auth/#users

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure: class FeatureType(models.Model): type = models.CharField(max_length=20) def __unicode__(self): return self.type
i have a Reply class: class Reply(models.Model): reply_to = models.ForeignKey(New) creator = models.ForeignKey(User) reply
I have a simple Book Author relationship class Author(models.Model): first_name = models.CharField(max_length=125) last_name =
I have an extended UserProfile model in django: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True)
I have a model.py: class usercommand(models.Model): user = models.ForeignKey(User, blank=False) a_field = models.PositiveIntegerField(null=True, blank=True)
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have 3 models class User < ... belongs_to :language has_many :posts end class
I have a comments model that belongs to two models: submissions and posts class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.