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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:35:33+00:00 2026-05-31T13:35:33+00:00

i need to shortlist Players from a specific team when i am entering a

  • 0

i need to shortlist Players from a specific team when i am entering a shortlisted players for a particuar match. my form field gives me a list of all players. I followed this to the letter

http://www.wkoorts.com/wkblog/2009/08/10/pre-populate-django-modelform-with-specific-queryset/

but it gives me attribute error. Here is my code forms.py

class ShortlistForm(ModelForm):
    class Meta:
        model = PlayerShortlist
        fields = (
            'player',
        )

and my view.py

def shortlist(request, team_id, fixture_id):
    template = get_template('cricket/shortlist.html')
    loggedinuser = request.user
    userteam = Team.objects.get(owner=loggedinuser)
    form = ShortlistForm
    #get the players only belonging to this team_id
    form.fields['player'].queryset = Player.objects.filter(team=userteam)
    currentteam = Team.objects.get(id=team_id)
    page_vars = Context({
        'form': form,
        'loggedinuser': loggedinuser,
        'team': userteam
    })
    output = template.render(page_vars)
    return HttpResponse(output)

if i remove the line

form.fields['player'].queryset = Player.objects.filter(team=userteam)

it gives me this error.

AttributeError at /team/1/fixture/1/shortlist/
type object 'ShortlistForm' has no attribute 'fields'

what i am i doing wrong?

//mouse

and while we are at it, if i can list all the players with accompnying check boxes, to be put in the database, rather boss have a dropdown .. what should i choose from the formset?

//mouse

again, solved it myself.

need to initiate form first.

form = ShortlistForm

should be

form = ShortlistForm()

//mouse

  • 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-31T13:35:33+00:00Added an answer on May 31, 2026 at 1:35 pm

    Quite a bit here:

    1. Why are you assigning request.user to loggedinuser? It’s exactly the same amount of keystrokes, so you aren’t saving yourself anything and only abstracting logic and adding points of failure to your code.

    2. When assigning the form you’re assigning the class, not an instance of that class. You need instead:

      form = ShortlistForm()
      
    3. When defining currentteam you need to catch the possibility of an incorrect team_id being passed in. The easiest way is with get_object_or_404:

      currentteam = get_object_or_404(Team, id=team_id)
      
    4. Unless Team.owner is a OneToOneField, you’re going to run into issues with Team.owner.get(owner=loggedinuser). It’s also confusing as to why you’re fetching two different teams here. If you just want to filter Players to those that belong to teams that belong to the current user, the following is far more simplistic and safer:

      Player.objects.filter(team__owner=request.user)
      
    5. However, why you’re doing this makes no sense. The comment you have says it should be filtered for the current team_id, and that makes sense based on the URL containing a team_id parameter. I’m assuming you’re simply wanting to filter by the current team but also want to make sure that that team is owned by the logged in user (user has rights to it). If that’s the case, the following is what you actually need:

      team = get_object_or_404(Team, id=team_id, owner=request.user)
      ...
      Player.objects.filter(team=team)
      
    6. The whole bit with fetching your template, rendering it, and returning the response all manually, is unnecessary and amounts to code bloat. Just do:

      # Django 1.3+
      return render(request, 'cricket/shortlist.html', page_vars)
      

      OR

      # Django <1.3
      return render_to_response('cricket/shortlist.html', page_vars, RequestContext(request))
      

      Make page_vars a regular old dictionary, instead of an instance of Context, in that scenario. This also has the benefit of bring request into your template context, so you can use request.user there as well, instead of having to pass in loggedinuser.

    So, with that, here’s your new view:

    def shortlist(request, team_id, fixture_id):
        team = get_object_or_404(Team, id=team_id)
    
        if request.method == 'POST':
            form = ShortlistForm(request.POST)
            #get the players only belonging to this team_id
            form.fields['player'].queryset = Player.objects.filter(team=team)
            if form.is_valid():
                return HttpResponseRedirect('/path/to/next/view/')
        else:
            form = ShortListForm()
            #get the players only belonging to this team_id
            form.fields['player'].queryset = Player.objects.filter(team=team)
    
        return render(request, 'cricket/shortlist.html', {
            'form': form,
            'team': team,
        })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to take a paragraph of text and extract from it a list
I need an example of the shortest path of a directed cyclic graph from
Need to take a SELECT drop down list options and find if any of
need a quick help here. I have a series of hyperlinks all with the
I have a large list of LDAP DN's that are all related in that
I need to populate dictionary from array. I've done in three lines and i'm
In SQL statements, we often need to create a list of question marks that
I need to get the div containing the street address within the list. The
In my application I need short list of objects of class Person . Each
Okay, so I need to make C go the shortest path from A to

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.