首页 > 其他分享 >Django - form 使用 initial 参数,设置字段默认显示参数

Django - form 使用 initial 参数,设置字段默认显示参数

时间:2023-02-28 18:37:30浏览次数:48  
标签:py name form models initial 参数 class verbose

 

models.py

class Order(models.Model):
    LEIXING_CHOICES = (
        (0, 'leixing1'),
        (1, 'leixing2'),
        (2, 'leixing3'),
    )
    user = models.ForeignKey(verbose_name="User", to="User", on_delete=models.CASCADE, null=True)
    leixing = models.SmallIntegerField(verbose_name="类型", choices=LEIXING_CHOICES, default=0)
    name = models.CharField(verbose_name="简称", max_length=128, null=True, blank=True)

 

forms.py

class OrderForm(forms.ModelForm):

    class Meta:
        exclude = []
        model = Order

 

views.py

def get(request, *args, **kwargs):
    data = {
        'user': User.objects.get(id=1),
        'leixing': 2,
        'name': 'test1111'
    }
    form = OrderForm(instance=None, initial=data)
    return render(request, 'index.html', locals())

 

标签:py,name,form,models,initial,参数,class,verbose
From: https://www.cnblogs.com/chaoqi/p/17165439.html

相关文章