J
J
Jekson2020-10-01 15:21:59
Django
Jekson, 2020-10-01 15:21:59

Bypass the limit on the number of characters in the form?

There is a model with a field limited to 32 characters. In the admin panel, when I enter more than 32 characters in the form field, I get a restriction message. The point is that I need to enter IDs separated by commas in this field (up to 20 characters each). Then the data from the form is processed, split(',') is executed and the received data is sent to the database via bulk_upload.
I tried to override max_length in __init__ , but this made it possible to enter more characters but not save them.

How to get around this limitation without changing the max_length for the database?
models.py

class MyModels(models.Models):
    ....
    items = models.CharField(verbose_name=_(u'Item ID'), max_length=32)

forms.py

class ItemForm(ModelForm):
        class Meta:
            model = MyModel
            fields = ['items']
        
        def __init__(self, *args, **kwargs):
                super(ItemForm, self).__init__(*args, **kwargs)
                )
                self.fields["items"] = forms.CharField(
                    max_length=512,
                )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2020-10-01
@AstraVlad

I see two options so far:
1. Use a formset and enter several records at once (but not separated by commas).
2. Write the form yourself and parse the entered data in the view.
Entering more characters into the ModelForm field than the Model requires is like trying to pour two liters of beer into a liter can. Will not enter.

D
Dr. Bacon, 2020-10-01
@bacon

There is a model with a field limited to 32 characters.

this made it possible to enter more characters but not save them.

Turn on your brains already, max_length in the database makes a restriction, how do you want to save more in the database than you yourself registered with your own hands.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question