Django AdminForm field default value

前端 未结 5 1170
生来不讨喜
生来不讨喜 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:04

    I'm not too sure what you need to set email to, but You can set the initial values in lots of different places.

    Your function def init() isn't indented correctly which i guess is a typo? Also, why are you specifically giving the email form field a TextInput? It already renders this widget by default

    You can set the email's initial value in your form's initialized (def __ init __(self))

    (self.fields['email'].widget).initial_value = "something"
    

    or in the model.py

    email = models.CharField(default="something")
    

    or as you have in forms.py

    email = models.CharField(initial="something")
    

提交回复
热议问题