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 3697052
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:48:49+00:00 2026-05-19T04:48:49+00:00

I want to filter a class by providing a list first_name = [‘John’, ‘Lian’]

  • 0

I want to filter a class by providing a list

first_name = ['John', 'Lian']
employees = Employee.objects.filter(firstname in first_name)

In real case, the first_name is generated on the fly.

And now I received that 'global name 'firstname' is not defined' error.
I’m sure that there’s a field in Employee class called firstname.

How can I fix this?
Thanks for your help!

Model class

class Employee(models.Model):

    status= fields.BooleanField(default=True)
    title = fields.CharField(max_length=128)
    first_name = models.ManyToManyField('First_name', blank=True, null=True)

Notice: This isn’t real model but can represent this issue.

  • 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-05-19T04:48:49+00:00Added an answer on May 19, 2026 at 4:48 am

    Use the next line:

    employees = Employee.objects.filter(firstname__in = first_name)
    

    If first_name field is ManyToManyField (and First_name model contains field “name”), you should try something like this:

    first_names = ['John', 'Lian']
    employees = Employee.objects.filter(first_name__title__in = first_names)
    

    Another way:

    first_names = ['John', 'Lian']
    instances = [First_name.objects.get(title = title) for title in first_names]
    employees = Employee.objects.filter(first_name__in = instances)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to filter repeated elements in my list for instance foo = ['a','b','c','a','b','d','a','d']
I want to filter elements from a list of lists, and iterate over the
so lets say I have a simple class Final and I want to filter
I want to filter objects from the db by a property that comes from
I have a .infodiv class with say 4 elements. I want to filter out
I want to filter objects based in some conditions or if their parent is
I want to filter the selectable dates on a datepicker. I basically need to
I want to filter a java.util.Collection based on a predicate.
I want to filter e-mail headers received on my Postfix mail receiving server. I
I want to filter rows from a data.frame based on a logical condition. Let's

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.