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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:27:45+00:00 2026-06-09T14:27:45+00:00

I wanted some guidance if possible on how to make this more generic: def

  • 0

I wanted some guidance if possible on how to make this more generic:

def get_industry_choices(self):
    industries = Industry.objects.all().order_by('name')
    ind_arr = [(ind.id, ind.name) for ind in industries]

    return ind_arr

Basically this function will return choices as expected for the forms.ChoiceField. I need to do this in a few places, and wanted to make the function above more generic. I know how to get the industries = Industry.objects.all().order_by('name') to be generic, but the 2nd part is what I’m not sure about. When creating the tuples, it has (ind.id, ind.name). the ind.name can be any value depending on the model passed in (it may not always have name in the model).

I tried to read up on this in a few places including:

Passing functions with arguments to another function in Python?

The above resource shows how to do it using a function passed in, but that seems a bit overkill? If I have to pass a function as an argument anyway, whats the point of making it more generic with one more function?

[EDIT]

Basically I want to produce something similar to this:

TITLE_CHOICES=(
    (1, 'Mr.'),
    (2, 'Ms.'),
    (3, 'Mrs.'),
    (4, 'Dr.'),
    (5, 'Prof.'),
    (6, 'Rev.'),
    (7, 'Other'),
)

So when doing forms.ChoiceField I can pass in TITLE_CHOICES for example as the possible choices. The first value is the value I get when the form is submitted, the second value is what the user sees on form. I want to be able to programmatically create this with any model, I pass in the model name and one field in the above example, name. I want to create the tuple such that it is (id, name). But name could be replaced with anything in a different model…

  • 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-09T14:27:47+00:00Added an answer on June 9, 2026 at 2:27 pm

    It is hard to tell from your question, but I think the bit you are missing is getattr(). For example

    ind = something()
    for field in ['id', 'name']:
        print getattr(ind, field)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted some tool which could check all the standard violations that I make
This is going to be a quick discussion, but I just wanted some feedback
I wanted to make some simple file recovery software, where I want to try
This question is a bit opinionated, but I wanted some input. I have two
This came up during one of our retrospectives and wanted some additional feedback and
I'm writing my first MSpec specifications and I wanted some guidance. I left the
Just wanted some quick help I'm 90% sure i need to use the (this)
I've been researching this, but i wanted some input from the community in regards
I wanted some system where I didn't have to keep looking at the Exception
I would like to learn ASP.NET and just wanted some input as to which

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.