Note that there are some explanatory texts on larger screens.

plurals
  1. POMore Pythonic way to repeat some actions for a number of attributes
    primarykey
    data
    text
    <p>I have a Django app that allows the user to create variables and name them</p> <pre><code>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) </code></pre> <p>And then they are stored</p> <pre><code>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) </code></pre> <p>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</p> <pre><code>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'] </code></pre> <p>Is there any way I could do this a bit more pythonicaly like have a list is <code>settings.py</code> and loop over it:</p> <pre><code>USER_SETTABLE_VARIABLES = ['int1','int2','float1','float2','string1','string2'] </code></pre> <p>The same applies to the way I make sure that the data entered is unique before saving:</p> <pre><code>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 </code></pre> <p>I only ask because when I want to add to the model I don't want to be editing all these functions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload