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
Quite a bit here:
Why are you assigning
request.usertologgedinuser? 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.When assigning the
formyou’re assigning the class, not an instance of that class. You need instead:When defining
currentteamyou need to catch the possibility of an incorrectteam_idbeing passed in. The easiest way is withget_object_or_404:Unless
Team.owneris aOneToOneField, you’re going to run into issues withTeam.owner.get(owner=loggedinuser). It’s also confusing as to why you’re fetching two different teams here. If you just want to filterPlayersto those that belong to teams that belong to the current user, the following is far more simplistic and safer: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 ateam_idparameter. 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:The whole bit with fetching your template, rendering it, and returning the response all manually, is unnecessary and amounts to code bloat. Just do:
OR
Make
page_varsa regular old dictionary, instead of an instance ofContext, in that scenario. This also has the benefit of bringrequestinto your template context, so you can userequest.userthere as well, instead of having to pass inloggedinuser.So, with that, here’s your new view: