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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:47:22+00:00 2026-05-26T12:47:22+00:00

I have a Django app that allows the user to create variables and name

  • 0

I have a Django app that allows the user to create variables and name them

class Product(models.Model):
    name = models.CharField(max_length=40, unique=True)
    int1_name = models.CharField(max_length=60, blank=True, null=True)
    int1_default = models.IntegerField(blank=True, null=True)
    int2_name = models.CharField(max_length=60, blank=True, null=True)
    int2_default = models.IntegerField(blank=True, null=True)
    float1_name = models.CharField(max_length=60, blank=True, null=True)
    float1_default = models.FloatField(blank=True, null=True)
    float2_name = models.CharField(max_length=60, blank=True, null=True)
    float2_default = models.FloatField(blank=True, null=True)
    string1_name = models.CharField(max_length=60, blank=True, null=True)
    string1_default = models.CharField(max_length=60, blank=True, null=True)
    string2_name = models.CharField(max_length=60, blank=True, null=True)
    string2_default = models.CharField(max_length=60, blank=True, null=True)

And then they are stored

class ItemData(models.Model):
    created = models.DateTimeField(default=datetime.now)
    item = models.ForeignKey(Item, editable=False)
    int1_val = models.IntegerField(blank=True, null=True)
    int2_val = models.IntegerField(blank=True, null=True)
    float1_val = models.DecimalField(blank=True, null=True, max_digits=12, decimal_places=2)
    float2_val = models.DecimalField(blank=True, null=True, max_digits=12, decimal_places=2)
    string1_val = models.CharField(max_length=60, blank=True, null=True)
    string2_val = models.CharField(max_length=60, blank=True, null=True)

And at the moment when I present the user with a form (that they have created) to fill in I do the following to lable up the fields with the names that user has given there variables

class ItemDataForm(ModelForm):

    # Only renames the fields based on whether the product has a name for the field
    def __init__(self,product,*args,**kwargs):
        super(ItemDataForm, self).__init__(*args, **kwargs)
        # delete all the fields that will be automatically filled in when saving
        del self.fields['created']
        # if the values is set in the product
        if product.int1_name:
            self.fields['int1_val'].label = product.int1_name
            self.fields['int1_val'].value = product.int1_default
        else:
            del self.fields['int1_val']
        if product.int2_name:
            self.fields['int2_val'].label = product.int2_name
            self.fields['int2_val'].value = product.int2_default
        else:
            del self.fields['int2_val']
        if product.float1_name:
            self.fields['float1_val'].label = product.float1_name
            self.fields['float1_val'].value = product.float1_default
        else:
            del self.fields['float1_val']
        if product.float2_name:
            self.fields['float2_val'].label = product.float2_name
            self.fields['float2_val'].value = product.float2_default
        else:
            del self.fields['float2_val']
        if product.string1_name:
            self.fields['string1_val'].label = product.string1_name
            self.fields['string1_val'].value = product.string1_default
        else:
            del self.fields['string1_val']
        if product.string2_name:
            self.fields['string2_val'].label = product.string2_name
            self.fields['string2_val'].value = product.string2_default
        else:
            del self.fields['string2_val']

Is there any way I could do this a bit more pythonicaly like have a list is settings.py and loop over it:

USER_SETTABLE_VARIABLES = ['int1','int2','float1','float2','string1','string2']

The same applies to the way I make sure that the data entered is unique before saving:

def is_unique(self,latestSI):
    if (self.cleaned_data['int1_val'] == latestSI.int1_val and
        self.cleaned_data['int2_val'] == latestSI.int2_val and
        self.cleaned_data['float1_val'] == latestSI.float1_val and
        self.cleaned_data['float2_val'] == latestSI.float2_val and
        self.cleaned_data['string1_val'] == latestSI.string1_val and
        self.cleaned_data['string2_val'] == latestSI.string2_val):
        return False
    else:
        return True

I only ask because when I want to add to the model I don’t want to be editing all these functions.

  • 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-26T12:47:22+00:00Added an answer on May 26, 2026 at 12:47 pm

    Don’t use exec for this! getattr is perfectly adequate, much safer, and readable too.

    class ItemDataForm(ModelForm):
        def __init__(self,product,*args,**kwargs):
            super(ItemDataForm, self).__init__(*args, **kwargs)
            del self.fields['created']
            fields = 'int1', 'int2', 'float1', 'float2', 'string1', 'string2'
            for field in fields:
                val = getattr(product, field + '_name')
                fval = field + '_val'
                if val:
                    self.fields[fval].label = val
                    self.fields[fval].value = getattr(product, field + '_default')
                else:
                    del self.fields[fval]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have written a Django app that makes use of Python threading to create
I have a django app with models as follows: A Question model An Answer
I have a form in my Django app (not in admin) that allows staff
I am working on a simple django app that allows users to create posts
I have a Django app that lets a user upload a file and does
I have a Django app that gets it's data completely from an external source
I have a Django app that use a django-tagging. I need to port this
This is probably a setting error somewhere. I have a django app that works
I'm using Google App Engine and Django templates. I have a table that 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.