title: Filtering Django User Choice Field use for a form
body: <p>I am working on a hard django project and I am stuck again. I have a field in the userprofile which is called troop:</p>
  
  <pre><code>class UserProfile(models.Model):
  
      scout_username = models.OneToOneField(User, on_delete=models.CASCADE)
      Group_Choice = Groups.Scout_Groups()
      troop = models.SlugField(max_length=27, choices=Group_Choice, default='None', blank=False)
      date_of_birth = models.DateField(default=date.today)
  
  
      def __str__(self):
          return '%s'% (self.scout_username)
  
  def create_profile(sender, **kwargs):
      if kwargs['created']:
          user_profile = UserProfile.objects.create(user=kwargs['instance'])
  
  post_save.connect(create_profile, sender=User)
  </code></pre>
  
  <p>Then I have a form which fills in data which is sent to my stData model. Within the form the user can choose to add details about another user. Except they can only add details to another user who has the same <code>troop</code> details. 
  forms.py</p>
  
  <pre><code>from django import forms
  from leaders.models import stData
  from django.contrib.auth.models import User
  from accounts.models import UserProfileManager, UserProfile
  
  st_username_list=[
      (None, 'Choose a user'),
      (user1, 'user1'),
      (i, 'no progress'),
      ]
  
  class BadgeForm(forms.ModelForm):
      def set_user(self, user):
          global st_username_list
          troop = user.userprofile.troop
          userprofile = UserProfile.objects.all()
          selected_st = userprofile.filter(troop=troop)
          for st in selected_st:
              username = str(st.st_username)
              st_username_list.append((username, username))
      st_username = forms.ChoiceField(choices=st_username_list)
      class Meta:
          model = stData
          fields = ('st_username', 'Pioneer_Badge', 'Explorer_Badge', 'Adventurer_Badge', 'Proficiency_Badge', 'Other_Badge') 
  </code></pre>
  
  <p><strong>Please note</strong>
  In the example above I used a global variable. I understand this is far from desired. I have since removed it thanks to the explanation of the proper way to do the filter (found after the line break). I'm only keeping this for education reasons for others who may find they had similar problems.</p>
  
  <p>I pass through the user within my views like this:</p>
  
  <pre><code>user = request.user
  user_form_setting = BadgeForm.set_user(self, user)
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  
  from accounts.st_badge_list import st_List
  
  class stData(models.Model):
  
      Pioneer_Choices = st_List.Target_Badges()
      Blue_Choices = st_List.Target_Badges()
      Black_Choices = st_List.Target_Badges()
      Proficiency_Choices = st_List.Proficiency_Badges()
      Other_Choice = st_List.Other_Badges()
  
      Pioneer_Badge = models.CharField(max_length=16, choices=Pioneer_Choices, default='None', blank=True)
      Blue_Star = models.CharField(max_length=16, choices=Blue_Choices, default='None', blank=True)
      Black_Star = models.CharField(max_length=16, choices=Black_Choices, default='None', blank=True)
      Proficiency_Badge = models.CharField(max_length=22, choices=Proficiency_Choices, default='None', blank=True)
      Other_Badge = models.CharField(max_length=27, choices=Other_Choice, default='None', blank=True)
  
  
      st_username = models.ForeignKey(User, on_delete=models.CASCADE)
      date = models.DateTimeField(auto_now=True)
      print (User)
  
      def __str__(self):
          return '%s'% (self.st_username)
  </code></pre>
  
  <p>How would I go about having it so whatever user has the same <code>troop</code> details will appear within the st_username_list as a choice? 
  After researching and trying things with the code, I have been getting:
  <code>ValueError
  Cannot assign "'user1'": "stData.st_username" must be a "User" instance.</code>
  I hope this is not too confusing.</p>
  
  <hr>
  
  <p><strong>Edit</strong>
  Ok so I have found that I can filter the options for the st_username by doing</p>
  
  <pre><code>def __init__(self, *args, **kwargs):
  
          super().__init__(*args, **kwargs)
          self.fields['st_username'].queryset = UserProfile.objects.filter(troop='''user's troop''')
  </code></pre>
  
  <p><strong>Problem update</strong>
  My main issue now is that I am unable to pass through the user instance within the model. I have seen this <a href="https://stackoverflow.com/questions/7299973/django-how-to-access-current-request-user-in-modelform">question here</a>. So I added this to my form's innit method:</p>
  
  <pre><code>    self.user = kwargs.pop('user')
  </code></pre>
  
  <p>Yet when I try and use the user by going <code>self.user</code> I get the an unhelpful error KeyError saying <code>user</code>. The shell indicated this may be due to the <code>self.user = kwargs.pop(user)</code>
  I believe this may be because I am not passing through the user. So when I call the form in my views, I tried <code>form = BadgeForm(user=request.user)</code> and got the same error.</p>
  
  <p>my queryset looks like this now:</p>
  
  <pre><code>self.fields['scout_username'].queryset=UserProfile.objects.filter(troop=user.userprofile.troop)
  </code></pre>
  
  <p><strong>Further Information:</strong>
  To understand the problem better, I have passed through a set variable of the troop within the queryset. So in this case</p>
  
  <pre><code>self.fields['scout_username'].queryset=UserProfile.objects.filter(troop='BC')
  </code></pre>
  
  <p>Although now I get Error AttributeError:
      'BadgeForm' object has no attribute '<strong>name</strong>'
  The shell links this with the formset from which I use the form with. The details I'm provided is:</p>
  
  <pre><code>line 435, in formset_factory
      return type(form.__name__ + 'FormSet', (formset,), attrs)
  </code></pre>
  
  <p>I hope this makes more sense to you than to me! Any help would be greatly appreciated!</p>