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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:53:11+00:00 2026-06-10T04:53:11+00:00

Say I have a model like: class Team(models.Model): name = models.CharField(max_length=20) class Game(models.Model): title

  • 0

Say I have a model like:

class Team(models.Model):
    name = models.CharField(max_length=20)

class Game(models.Model):
    title = models.CharField(ma_length=20)
    home_team = models.ForeignKey(Team)
    away_team = models.ForeignKey(Team)

Then in my view I have:

def manage_games(request):
     GameFormSet = modelformset_factory(Game, extra=1)
     game_forms = GameFormSet(request.POST or None,
                                 queryset=Game.objects.all())
     if request.method == "POST":
         if game_forms.is_valid():
            game_forms.save()
            game_forms = GameFormSet(queryset=Game.objects.all())                 

     return render(request, "admin_dashboard/manage_games.html", locals())

This works okay except that I’d like to be able to turn the home_team and away_team into CharFields (rather than the dropdown it currently is) while also keeping them separate models. How can I add an inline model formset into a model formset to make this possible?

  • 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-06-10T04:53:13+00:00Added an answer on June 10, 2026 at 4:53 am

    An inlinemodelformset wouldn’t work because Game has the foreignkeys Team.

    An inlinemodelformset can show a list of forms for models which are connected to another via a ForeignKey. For example if A.b is an FK to B, then it is possible to make an inlinemodelformset of A for B.

    However, you could still add a couple of fields to change the home/away team names:

    from django import forms
    
    from models import Team, Game    
    
    
    class GameUpdateForm(forms.ModelForm):
        home_team_name = forms.CharField(max_length=20)
        away_team_name = forms.CharField(max_length=20)
    
        def save(self):
            obj = super(GameForm, self).save()
    
            if not obj.home_team_id:
                obj.home_team = Team()
            obj.home_team.name = self.cleaned_data.get('home_team_name')
            obj.home_team.save()
    
            if not obj.away_team_id:
                obj.away_team = Team()
            obj.away_team.name = self.cleaned_data.get('away_team_name')
            obj.away_team.save()
    
            return obj
        class Meta:
            model = Game
            exclude = ('home_team', 'away_team')
    

    And make the modelformset use it:

    GameFormSet = modelformset_factory(Game, form=GameForm, extra=1)
    

    You might also want to add CharField autocompletion.

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

Sidebar

Related Questions

So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):
Say I have a Django class something like this: class Person(models.Model): name = models.CharField(max_length=50)
say I have such model: class Foo(models.Model): name = models.CharField(name,max_length=25) type = models.IntegerField(type number)
Let's say I have two models looking like this: class ProductType(models.Model): product_type = models.CharField(max_length=100)
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
Say I have a model like this: class Book(models.Model): user = models.ForeignKey(User) book_isbn =
Say I have a model that looks like: class Action(models.Model): user = models.ForeignKey('auth.User') action
Say I have a model that looks like: class StockRequest(models.Model): amount_requested = models.PositiveIntegerField(null=True) amount_approved
Let's say i have many models like that: class ExampleModel(models.Model): photo = models.ImageField(upload_to='photos/') image
Say I have django model that looks something like this: class Order(models.Model): number =

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.