Make the django form foreign key field read-only and still allow form submission

How to make a foreign key field in a form read-only, but still allow this field to be valid after the form is submitted? According to the W3C, disabled fields are not taken into account after submitting the form ... using the code below, I can set the field as disabled, this way is read-only, but my form does not go through

    def __init__(self, *args, **kwargs):
       super(IssuesForm, self).__init__(*args, **kwargs)
       self.fields['vehicle'].widget.attrs['readonly'] = True

Ideas ....?

+3
source share
4 answers

Django Python, = "hidden" , . - , .

+2

, ... , , , .

0

, JavaScript

0

, , , . , , "" , , - .

class EditExifForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(EditExifForm, self).__init__(*args, **kwargs)
        self.fields['image'].widget.attrs['hidden'] = True    # This is the solution
        # Setting as 'readonly' didn't make a difference
        # Setting as 'disabled' made the form not update the database

    class Meta:
        model = exif
        ...
0
source

Source: https://habr.com/ru/post/1712226/


All Articles