0

Within the model "Example" I have two fields:

-"description" (varchar, length 64). -"description_length" integer. 

description_length has different value for each record, and it is always between 1 and 64.

In a Django form how is possible set the maxlength description equal to the description_length variable value?

1 Answer 1

2

Sure thing.

class ExampleForm(forms.ModelForm): class Meta: model = Example def __init__(self, *args, **kwargs): super(ExampleForm, self).__init__(*args, **kwargs) if self.instance: self.fields['description'].max_length = self.instance.description_length 

Just be sure your description model field is big enough to accomodate the text of description_length:

class Example(models.Model): MAX_DESCRIPTION_LENGTH = 1024 description = models.CharField(max_length=MAX_DESCRIPTION_LENGTH) description_length = models.IntegerField( validators=MaxValueValidator(MAX_DESCRIPTION_LENGTH)) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.