Ada*_*son 3 django django-forms
我有一个模型:
CAMPAIGN_TYPES = (
('email','Email'),
('display','Display'),
('search','Search'),
)
class Campaign(models.Model):
name = models.CharField(max_length=255)
type = models.CharField(max_length=30,choices=CAMPAIGN_TYPES,default='display')
Run Code Online (Sandbox Code Playgroud)
一个表格:
class CampaignForm(ModelForm):
class Meta:
model = Campaign
Run Code Online (Sandbox Code Playgroud)
有没有办法限制"类型"字段可用的选项?我知道我可以做一个单值字段:CampaignForm(initial={'name':'Default Name'})
但我找不到任何方法为选择集做这个.
这就是我如何限制显示的选项:
在forms.py中为表单添加init方法
class TaskForm(forms.ModelForm):
....
def __init__(self, user, *args, **kwargs):
'''
limit the choice of owner to the currently logged in users hats
'''
super(TaskForm, self).__init__(*args, **kwargs)
# get different list of choices here
choices = Who.objects.filter(owner=user).values_list('id','name')
self.fields["owner"].choices = choices
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4031 次 |
最近记录: |