Django AdminForm field default value

前端 未结 5 1143
生来不讨喜
生来不讨喜 2020-12-29 06:40

I have a Django admin form. And now I want to fill it\'s initial field with data based on my model. So I tried this:

class OrderForm(forms.ModelForm):
    cl         


        
5条回答
  •  有刺的猬
    2020-12-29 07:22

    This code is worked for me (Django 1.11):

    from django import forms
    
    class MyAdminForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.initial['field_name'] = 'initial_value'
    

提交回复
热议问题