gpt4 book ai didi

python - 如何在 django ModelForm 外键中设置默认选定值

转载 作者:行者123 更新时间:2023-11-30 23:18:59 24 4
gpt4 key购买 nike

我想在 ModelForm 中的模型的ForeignKeyField 上设置预选的选择选项。以下是我的模型和表格。我只想为 ModelForm 中的 State 字段设置预选的 value=1 。这样它就会向用户显示为已选择的选项。

Customer Model:


class Customer(models.Model):
"""
Customer model
"""

GENDER_CHOICES = (('M', 'M'),
('F', 'F'))
SDW_CHOICES = (('S/o', 'S/o'),
('D/o', 'D/o'),
('W/o', 'W/o'))

name = models.CharField(verbose_name='customer name', max_length=50,
help_text='max length 50 characters')
guardian_name = models.CharField(verbose_name='customer guardian name', max_length=50,
help_text='max length 50 characters')
phone_no_1 = models.CharField(verbose_name='customer phone number 1', max_length=10, null=True, blank=True,
help_text='max length 10 digits')
phone_no_2 = models.CharField(verbose_name='customer phone number 2', max_length=10, null=True, blank=True,
help_text='max length 10 digits')
address = models.CharField(verbose_name='customer address', max_length=150,
help_text='max length 150 characters')
state = models.ForeignKey(State, help_text='Please select from list')
district = models.ForeignKey(District, help_text='Please select from list')
village_or_city = models.ForeignKey(VillageOrCity, help_text='Please select from list')
created_on = models.DateTimeField(verbose_name='customer added on', auto_now_add=True)
updated_on = models.DateTimeField(verbose_name='customer last modified', auto_now=True)
remark = models.TextField(max_length=150, null=True, blank=True, help_text='max length 150 characters')
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, help_text='Please select from list')
image = models.ImageField(upload_to=MEDIA_ROOT, default=DEFAULT_USER_IMAGE)
sdw = models.CharField(verbose_name='Son/daughter/wife of', max_length=3, choices=SDW_CHOICES,
help_text='Please select from list')
caste = models.ForeignKey(Caste, help_text='Please select from list')
created_by = models.ForeignKey(User, related_name='created_by', null=True)
updated_by = models.ForeignKey(User, related_name='updated_by', null=True)


CreateCustomerForm:

class CreateCustomerForm(forms.ModelForm):
"""
CreateCustomerForm class. Renders Customer model form
"""

class Meta:

model = Customer
fields = ('name', 'sdw', 'guardian_name', 'phone_no_1',
'phone_no_2', 'address', 'state', 'district',
'village_or_city', 'remark', 'gender', 'caste',
'image'
)
widgets = {
'name': forms.TextInput(attrs={'placeholder': 'Enter customer name',
'required': 'required',
'autofocus': 'autofocus', 'size': "50"
}
),
'sdw': forms.Select(attrs={'required': 'required',
}
),
'guardian_name': forms.TextInput(attrs={'placeholder': 'Enter guardian name',
'required': 'required', 'size': "50"
}
),
'phone_no_1': forms.TextInput(attrs={'placeholder': 'Enter phone number',
'size': "50"
}
),
'phone_no_2': forms.TextInput(attrs={'placeholder': 'Enter phone number',
'size': "50"
}
),
'address': forms.TextInput(attrs={'placeholder': 'Enter address',
'required': 'required', 'size': "50"
}
),
'state': forms.Select(attrs={'required': 'required',
}
),
'district': forms.Select(attrs={'required': 'required',
}
),
'village_or_city': forms.Select(attrs={'required': 'required',
}
),
'remark': forms.Textarea(attrs={'placeholder': 'Enter remark',
}
),
'gender': forms.Select(attrs={'required': 'required',
}
),
'caste': forms.Select(attrs={'required': 'required',
}
),
}

最佳答案

使用initial在Django的CBV中设置初始数据。

class CreateCustomerView(LoginRequiredMixin, CreateView):
"""
Handles request for '/add/customer/'
"""

initial = {'state': '1'
}
form_class = CreateCustomerForm
template_name = CREATE_CUSTOMER_TEMPLATE

它将使用预先填充的数据呈现模板。

关于python - 如何在 django ModelForm 外键中设置默认选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26373678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com