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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:39:54+00:00 2026-05-26T21:39:54+00:00

I have the following models: # Group for Key/Value pairs class Group(models.Model): name =

  • 0

I have the following models:

# Group for Key/Value pairs
class Group(models.Model):
    name = models.TextField(unique=True)

    def __unicode__(self):
        return self.name


    class Meta:
        verbose_name = 'Group'
        verbose_name_plural = 'Groups'

# Key is the key/name for a Value
class Key(models.Model):
    name = models.TextField(unique=True)

    def __unicode__(self):
        return self.name


    class Meta:
        verbose_name = 'Key'
        verbose_name_plural = 'Keys'

# Value is the value/content of a key
class Value(models.Model):
    value = models.TextField()

    def __unicode__(self):
        return '%s' % self.value


    class Meta:
        verbose_name = 'Value'
        verbose_name_plural = 'Values'

class Key_Value(models.Model):
    group = models.ForeignKey(Group)
    key = models.ForeignKey(Key)
    value = models.ForeignKey(Value)

    def __unicode__(self):
        return '%s = %s' % (self.key.name, self.value.value)


    class Meta:
        verbose_name = 'Key/Value Paar'
        verbose_name_plural = 'Key/Value Paare'


Now I pass the form to the template:
def exampleview(request):
    key_value_form = Key_Value_Form(request.POST)
    return render_to_response(
            'edit.html', {
                'key_value_form': key_value_form,
                })

Now lets look at possible data

KEY/VALUE PARIRS:
key = TEST 1
value = TEST 1
group = TESTGROUP 1

key = TEST 2
value = TEST 2
group = TESTGROUP 2

Now I changed the default widgets for the Key/Value Table entries to select widgets.

Here’s what I want to do:

SELECT GROUP [+] [-]
   --> [Now choose Key/Value pair belonging to group] [+] [-]

at the start you always get two selects one for the group and one for the key/value pair.
if you press the + at the GROUP a new group select button should appear along with a KEY/Value Pair select, if you press the + at the Key/Value select a new key/value select box should appear.

I have two problems:

ONE: I don’t know how the check in the template should look like and
TWO: How I can implement those + – Buttons

Any Help is appreciated. It would be cool if this would be possible without javascript but I don’t have very high hopes in that direction

  • 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-26T21:39:54+00:00Added an answer on May 26, 2026 at 9:39 pm

    You need to build the form on the fly. Suppose you had a definition of a group form built in a dictionary from a view in your view handler (containing group model foreign keys and number of actual keys in them):

    # note that the dictionary d shown here is just an example
    # yours should have actual group foreign keys based on Group model
    d = { 'first' : 5,  'second' : 3, 'third' : 2 }
    
    def make_groupkeys_form(descriptor):
        groups = { 'select': '', 'keypairs' : [] }
        fields = { 'groups' : groups }
    
        for group, numkeys in descriptor.values():
            groupchoices = # find out what choices you need to offer for group widget
            groups['select'] = forms.SelectField(choices=groupchoices)
            for i in range(numkeys):
                 keyvalchoices = # find out what choices you need to offer for keyval widget per group
                 groups['keypairs'].append(forms.SelectField(choices=keyvalchoices))
    
        # now you have enough SelectFields for all your groups and keyval pairs
        return type('Key_Value_Form', (forms.BaseForm,), { 'base_fields': fields })
    
    # from view handler
    def exampleview(request):
        # determine from request how many groups you want and how many keyval widgets per group, for example here I will use the above predefined d
    
        key_value_form = make_groupkeys_form(d)
        return render_to_response(
                'edit.html', {
                    'key_value_form': key_value_form,
                    })
    

    Note that you can (and should) rewrite a class Key_Value_Form to inherit from forms.BaseForm and put the make_groupkeys_form(descriptor) code in its __init__(request, descriptor) member. You will also need to do write your own is_valid() to enumerate through select fields and make sure the choices are correct when the user submits the form and override clean() to attempt to validate individual user’s choices.

    Finally, consider going through this dynamic forms read. It will guide you step by step in how to create dynamic forms in django.

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

Sidebar

Related Questions

I have the following Model: class Group(models.Model): member = models.ManyToManyField(Player, through='GroupMember') name = models.CharField(max_length=20,
I have the following model in my Django app: class Group(models.model): name=models.CharField(max_length=30) users=Models.ManyToManyField(User) In
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
I have the following models: class Fixture(models.Model): id = models.AutoField(primary_key=True) home_team = models.ForeignKey(Player, related_name=home)
Consider the following models: class Person(models.Model): name = models.CharField(max_length=128) class Group(models.Model): name = models.CharField(max_length=128)
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =
Lets say, I have following models in my Django app. class EventGroup name =

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.