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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:26:21+00:00 2026-05-19T11:26:21+00:00

Hello everyone I just started with django and was wondering how to do the

  • 0

Hello everyone I just started with django and was wondering how to do the following behaivor in my models:

class Game(models.Model)
  characters_count = models.IntegerField(default=1) #basically i set to a choice of 1-3

class Match(models.Model)
 video=models.ForeignKey(Game)
 p1char1 = models.ForeignKey(Character)
 p2char1 = models.ForeignKey(Character)
 p1char2 = models.ForeignKey(Character)
 p2char2 = models.ForeignKey(Character)
 p1char3 = models.ForeignKey(Character)
 p2char3 = models.ForeignKey(Character)

video contains the instance game which Match relies on.

I need to create a constraint that the Character selected is from the given game and that the number of characters each player selects is limited by game as well.

Should I perform this on the Save method? or can I set these constraints some other way so the forms will be generated by the models accordingly without doing it manually.

Will the limitation on the characters (by the game) can be done on the definition of the model?

  • 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-19T11:26:22+00:00Added an answer on May 19, 2026 at 11:26 am

    for your characters you should create a Many to Many relation with your character Model.

    Then override Match save() method to constrain the number of characters selected.

    Another way would be to put this constraint in the clean() method of a modelform based on match : so they have a list to choose from (use a checkbox list widget) and if they select too much character, the form will relaod with the error message you’ll put in clean()

    To limit the character depending on the game, you can add another M2M relation between Character and Game (‘allowed_in_game’) : in the admin, you’ll be able to choose the characters from a game.

    Then, in the Match Modelform, you’ll have to modifiy the ModelchoiceField showing characters to filter them to show only the ones which are related to the current Game.

    Here’s how (just borrowed from here) :

    class Matchform(forms.ModelForm):
        class Meta:
            model = Match
    
         def __init__(self, game, *args, **kwargs):
             super(Matchform, self).__init__(*args, **kwargs)
             self.fields['character'].queryset = Character.objects.filter(allowed_in_game__in = game)
    

    Then you can call your modified ModelForm like this :

    Matchform(game)
    Matchform(game,request.POST)
    Matchform(game,instance=m)
    

    You can see that everytime the “game” parameter is added, it should be a Game object for this example to work.

    Hope this helps….

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

Sidebar

Related Questions

Hello everyone I was just going trough http://static.springsource.org/docs/Spring-MVC-step-by-step/part1.html spring tutorial, and I thought its
Hello everyone I'm trying to work with jboss messaging, does anyone knows the default
Hello everyone here is my DAO class : public class UsersDAO extends HibernateDaoSupport {
Hello everyone I am just learning my first programming language and have just corrected
Hello everyone i just picked up jQuery So my question is i want to
hello everyone I am newto programming and I am just a little bit confused.
Hello everyone I'm trying to make a jquery Menu just like the one in
Hello everyone and thank you for your time. I would just like to say
Hello everyone i am new to rails just wanted to ask What this line
Hello everyone. I am trying to develop a Jquery plugin following the steps I

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.