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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:48:51+00:00 2026-05-25T15:48:51+00:00

I have a simple form in Django that looks like this: class SettingForm(forms.Form): theme

  • 0

I have a simple form in Django that looks like this:

class SettingForm(forms.Form):
    theme = forms.CharField(rrequired=True,
        initial='multgi'
    )
    defaultinputmessage = forms.CharField(required=True,
        initial='Type here to begin..'
    )

…and the model to store it looks like:

class Setting(models.Model):
    name = models.CharField(
        null=False, max_length=255
    )
    value= models.CharField(
        null=False, max_length=255
    )

When the form is submitted, how can i store the form fields as key value pairs and then when the page is rendered, how can I initialize the form with the key’s value. I’ve tried looking for an implementation of this but have been unable to find one.

Any help?

Thanks.

  • 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-25T15:48:52+00:00Added an answer on May 25, 2026 at 3:48 pm

    Here’s how I did it.

    I needed to do this because I had a Model that stored information as key value pairs and I needed to build a ModelForm on that Model but the ModelForm should display the key-value pairs as fields i.e. pivot the rows to columns. By default, the get() method of the Model always returns a Model instance of itself and I needed to use a custom Model. Here’s what my key-value pair model looked like:

    class Setting(models.Model):
        domain = models.ForeignKey(Domain)
        name = models.CharField(null=False, max_length=255)
        value = models.CharField(null=False, max_length=255)
    
        objects = SettingManager()
    

    I built a custom manager on this to override the get() method:

    class SettingManager(models.Manager):
    
        def get(self, *args, **kwargs):
            from modules.customer.proxies import *
            from modules.customer.models import *
    
            object = type('DomainSettings', (SettingProxy,), {'__module__' : 'modules.customer'})()
            for pair in self.filter(*args, **kwargs): setattr(object, pair.name, pair.value)
    
            setattr(object, 'domain', Domain.objects.get(id=int(kwargs['domain__exact'])))
            return object
    

    This Manager would instantiate an instance of this abstract model. (Abstract models don’t have tables so Django doesn’t throw up errors)

    class SettingProxy(models.Model):
    
        domain = models.ForeignKey(Domain, null=False, verbose_name="Domain")
        theme = models.CharField(null=False, default='mytheme', max_length=16)
        message = models.CharField(null=False, default='Waddup', max_length=64)
    
        class Meta:
            abstract = True
    
        def __init__(self, *args, **kwargs):
            super(SettingProxy, self).__init__(*args, **kwargs)
            for field in self._meta.fields:
                if isinstance(field, models.AutoField):
                    del field
    
        def save(self, *args, **kwargs):
            with transaction.commit_on_success():
                Setting.objects.filter(domain=self.domain).delete()
    
                for field in self._meta.fields:
                    if isinstance(field, models.ForeignKey) or isinstance(field, models.AutoField):
                        continue
                    else:
                        print field.name + ': ' + field.value_to_string(self)
                        Setting.objects.create(domain=self.domain,
                            name=field.name, value=field.value_to_string(self)
                        )
    

    This proxy has all the fields that I’d like display in my ModelFom and store as key-value pairs in my model. Now if I ever needed to add more fields, I could simply modify this abstract model and not have to edit the actual model itself. Now that I have a model, I can simply build a ModelForm on it like so:

    class SettingsForm(forms.ModelForm):
    
        class Meta:
            model = SettingProxy
            exclude = ('domain',)
    
        def save(self, domain, *args, **kwargs):
            print self.cleaned_data
            commit = kwargs.get('commit', True)
            kwargs['commit'] = False
            setting = super(SettingsForm, self).save(*args, **kwargs)
            setting.domain = domain
            if commit:
                setting.save()
            return setting
    

    I hope this helps. It required a lot of digging through the API docs to figure this out.

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

Sidebar

Related Questions

Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
I have a simple Django form: class CommentForm(forms.Form): comment = forms.CharField(max_length=2000, required=True) post_id =
I have this simple form: class PagoDesde(forms.Form): from django import forms as f desde
I have a simple form that looks like so <% remote_form_for post, :url =>
I have a pretty simple form: from django import forms class InitialSignupForm(forms.Form): email =
I have a simple form class MyForm(forms.Form): ... fieldname = forms.CharField(help_text=Some help text) I
I have a pretty simple file upload form class in django: class UploadFileForm(forms.Form): category
I have a simple form like this: <form name=serachForm method=post action=/home/search> <input type=text name=searchText
I have a page that has a simple form. When I submit this form
I have a simple form with some plain html input like bellow using ASP.NET

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.