proper subject He is interested in how to add something like title or maybe some data-attribute in the input of the entity being edited.

    1 answer 1

    If you need a static value, you can overload the Meta.widgets form and set the widget with the necessary attributes for the field:

     class SomeAdminForm(forms.ModelForm): model = MyModel Meta: widgets = { 'some_field': forms.TextInput(attrs={'data-something': 'some value'} } 

    To set a dynamic value, you need to overload the __init__ method of the form and initialize the widgets there:

     class SomeAdminForm(forms.ModelForm): model = MyModel def __init__(self, *args, **kwargs): super(SomeAdminForm, self).__init__(*args, **kwargs) self.fields['some_field'].widget = forms.TextInput(attrs={'data-something': self.instance.get_some_value()}) 
    • Which files should I put this code in? - ashalbulk 2:41 pm
    • @ashalbulk forms usually in forms.py - dizballanze
    • did everything as you said, but does not work. I noticed that he does not see forms.py at all. It seems to me that it is necessary somewhere in the settings to register something in order for django to start using this file. Googled but found nothing ( - ashalbulk
    • @ashalbulk for the admin to pick up the form you need to specify it in ModelAdmin - dizballanze
    • no, it didn't work, it just doesn't see forms.py. In ide, the class of the form in ModelAdmin is highlighted in red and gives 502. And if I prescribe some kind of heresy in forms.py nothing happens, it does not give errors. Therefore, I have come to that. that it is simply not connected, the file itself. Django 1.8 - ashalbulk