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

  • Home
  • SEARCH
  • 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 350045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:30:53+00:00 2026-05-12T11:30:53+00:00

My problem is similar to Django Passing Custom Form Parameters to Formset Ive have

  • 0

My problem is similar to Django Passing Custom Form Parameters to Formset

Ive have these classes

class Game(models.Model):
    home_team = models.ForeignKey(Team, related_name='home_team')
    away_team = models.ForeignKey(Team, related_name='away_team')
    round = models.ForeignKey(Round)

TEAM_CHOICES = ((1, '1'), (2, 'X'), (3, '2'),)

class Odds(models.Model):
    game = models.ForeignKey(Game, unique=False)
    team = models.IntegerField(choices = TEAM_CHOICES)
    odds = models.FloatField()
    class Meta:
        verbose_name_plural = "Odds"
        unique_together = (
            ("game", "team"),
        )

class Vote(models.Model):
    user = models.ForeignKey(User, unique=False)
    game = models.ForeignKey(Game)
    score = models.ForeignKey(Odds)
    class Meta:
        unique_together = (
            ("game", "user"),)

And I’ve defined my own modelformset_factory:

def mymodelformset_factory(ins):
    class VoteForm(forms.ModelForm):
        score = forms.ModelChoiceField(queryset=Odds.objects.filter(game=ins), widget=forms.RadioSelect(), empty_label=None)
        def __init__(self, *args, **kwargs):
            super(VoteForm, self).__init__(*args, **kwargs)
        class Meta:
            model = Vote
            exclude = ['user']
    return VoteForm 

And I use it like this:

        VoteFormSet = modelformset_factory(Vote, form=mymodelformset_factory(v), extra=0)
        formset = VoteFormSet(request.POST, queryset=Vote.objects.filter(game__round=round, user=user))

This displays the form:

drop down box of Game(s) in the Round specified and is supposed to display 3 radio buttons for the Odds but I don’t know what to pass as a parameter to the mymodelformset_factory.. If v = Game.objects.get(pk=1) it obviously only displays Game with pk=1 for ALL Games, What I need is v = Game.objects.get(pk=”game who is connected to the odds regarding”) if you catch my drift..

  • 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-12T11:30:53+00:00Added an answer on May 12, 2026 at 11:30 am

    I think you want to make some changes to your custom factory function. It should return the formset class, not the form. How about this:

    def make_vote_formset(game_obj, extra=0):
        class _VoteForm(forms.ModelForm):
            score = forms.ModelChoiceField(
                queryset=Odds.objects.filter(game=game_obj), 
                widget=forms.RadioSelect(), 
                empty_label=None)
    
            class Meta:
                model = Vote
                exclude = ['user',]
        return modelformset_factory(Vote, form=_VoteForm, extra=extra)
    

    Then in your view code:

    current_game = Game.objects.filter(id=current_game_id)
    VoteFormSet = make_vote_formset(current_game)
    formset = VoteFormSet(
                 request.POST, 
                 queryset=Vote.objects.filter(game__round=round, user=user))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I define a Django form class similar to this: def class MyForm(forms.Form): check
I seem to have similar problem as this guy: Django newbie deployment question -
I have similar problem to one discussed here , but with stronger practical usage.
I suppose similar problem would have been discussed here, but I couldn't find it.
I have a similar problem to this post . I need to display up
I notice this thread: Fastish Python/Jython IPC , and I have a similar problem,
I have a similar problem as a previously solved problem of mine, except this
I have a problem when upgrading a django and mysql app with south. I've
I have been struggling with a problem in Google App Engine and Django and
I have a really strange problem with the standard logging module used in django

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.