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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:03:49+00:00 2026-05-22T22:03:49+00:00

I need to pass an instance variable (self.rank) to be used by a class

  • 0

I need to pass an instance variable (self.rank) to be used by a class variable (provider) (see the commented out line below).

Commented out, the code below works. But I’m pretty sure I shouldn’t be trying to pass an instance variable up to a class variable anyway. So I’m dumbfounded as to how to accomplish my goal, which is to dynamically filter down my data in the ModelChoiceField.

As you can see, I already overrided ModelChoiceField so I could beautify the usernames. And I also subclassed my basic SwapForm because I have several other forms I’m using (not shown here).

Another way of saying what I need … I want the value of request.user in my Form so I can then determine the rank of that user and then filter out my Users by rank to build a smaller ModelChoiceField (that looks good too). Note that in my views.py, I call the form using:

form = NewSwapForm(request.user)

or

form = NewSwapForm(request.user, request.POST)

In forms.py:

from myapp.swaps.models import Swaps
from django.contrib.auth.models import User

class UserModelChoiceField(forms.ModelChoiceField):
    """ Override the ModelChoiceField to display friendlier name """
    def label_from_instance(self, obj):
    return "%s" % (obj.get_full_name())

class SwapForm(forms.ModelForm):
    """ Basic form from Swaps model. See inherited models below. """
    class Meta:
        model = Swaps

class NewSwapForm(SwapForm):
    # Using a custom argument 'user'
    def __init__(self, user, *args, **kwargs):
        super(NewSwapForm, self).__init__(*args, **kwargs)
        self.rank = User.objects.get(id=user.id).firefighter_rank_set.get().rank

    provider = UserModelChoiceField(User.objects.all().
            order_by('last_name').
            filter(firefighter__hirestatus='active')
            ### .filter(firefighter_rank__rank=self.rank)  ###
            )

    class Meta(SwapForm.Meta):
        model = Swaps
        fields = ['provider', 'date_swapped', 'swap_shift']

Thanks!

  • 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-22T22:03:49+00:00Added an answer on May 22, 2026 at 10:03 pm

    You can’t do it that way, because self doesn’t exist at that point – and even if you could, that would be executed at define time, so the rank would be static for all instantiations of the form.

    Instead, do it in __init__:

    provider = UserModelChoiceField(User.objects.none())
    
     def __init__(self, user, *args, **kwargs):
        super(NewSwapForm, self).__init__(*args, **kwargs)
        rank = User.objects.get(id=user.id).firefighter_rank_set.get().rank # ??
        self.fields['provider'].queryset = User.objects.order_by('last_name').filter(
               firefighter__hirestatus='active', firefighter_rank__rank=rank)
    

    I’ve put a question mark next to the rank line, because rank_set.get() isn’t valid… not sure what you meant there.

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

Sidebar

Related Questions

I need to pass a regex substitution as a variable: sub proc { my
I was using an mxml class but since i need to pass some properties
The code below works for the class that I hard coded XCCustomers in my
Why we need to pass the runnable instance while creating the threads using the
I need to pass an ID and a password to a batch file at
I need to pass an array from JavaScript to a page method in C#.
I'm creating a control and need to pass it the current logon user as
I have a java application I need to pass some info to a C++
I'm creating a function where I need to pass an object so that it
I have a user control in a repeater that I need to pass data

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.