2017-01-11 38 views
0

我有一个表单预填充页面加载用户字段与当前登录的用户。我想灰色这个领域,以便用户不能将其更改为未登录的用户。我希望用户能够更改其余的字段。我试过在模型表单和视图上使用init函数无济于事。任何指针将不胜感激。如何在django泛型FormView中使表单字段只读或不可编辑?

forms.py:

class PtoRequestForm(forms.ModelForm): 
    class Meta: 
     # Model to be used in the form. 
     model = PtoHistory 
     # Define which model fields to include in the form. 
     fields = [ 
      'user', 
      'start', 
      'end', 
      'leave_type', 
     ] 
     # Attach input widgets to fields for a friendlier user interface. 
     widgets = { 
      'start': DateTimeWidget(attrs={'id':'start'}, usel10n = True, bootstrap_version=3), 
      'end': DateTimeWidget(attrs={'id':'end'}, usel10n = True, bootstrap_version=3), 
    } 

views.py:

class IndexView(FormView): 
    template_name = 'accounts/index.html' 
    form_class = PtoRequestForm 
    success_url = '/accounts' 
    # Pre-populates the form with the specified values on page load. 
    def get_initial(self): 
     return {'user': self.request.user} 

    # Function runs when form is submitted. Do whatever needed to the form before it's saved to the db. 
    def form_valid(self, form): 
     form.save() 
     return super(IndexView, self).form_valid(form) 

    # Function runs on page load. Gather any information needed for the template. 
    def get_context_data(self, **kwargs): 
     # Initialize context 
     context = super(IndexView, self).get_context_data(**kwargs) 

     # Grab all ptoHistory records from the database, selecting only the values desired. 
     pto_history = PtoHistory.objects.values('start', 'end', 'pk', 'user_id') 
     # Convert the django queryset to json so that fullcalendar can work with it on the frontend. 
     json_pto_history = json.dumps(list(pto_history), cls=DjangoJSONEncoder) 
     # Save the converted json to the context to be returned to the front end. 
     context['ptoHistory'] = json_pto_history 
     return context 

这里是我的表格截图。用户不应该能够更改用户字段,但其余字段可以更改。

enter image description here

+0

可以覆盖\ __ __的init \ PtoRequestForm,并在其中添加self.fields [ '用户'] widget.attrs ['只读。 '] = True –

回答

0

在我的模型的形式我不包括像这样的 '用户' 字段:

forms.py:

class PtoRequestForm(forms.ModelForm): 
    class Meta: 
     # Model to be used in the form. 
     model = PtoHistory 
     # Define which model fields to include in the form. 
     fields = [ 
      'start', 
      'end', 
      'leave_type', 
     ] 
     # Attach input widgets to fields for a friendlier user interface. 
     widgets = { 
      'start': DateTimeWidget(attrs={'id':'start'}, usel10n = True, bootstrap_version=3), 
      'end': DateTimeWidget(attrs={'id':'end'}, usel10n = True, bootstrap_version=3), 
     } 

然后在我的form_valid函数中,我只附加了当前登录的用户b安伏我把它保存到数据库中,像这样:

views.py:

def form_valid(self, form): 
     new_form = form.save(commit=False) 
     new_form.user = self.request.user 
     new_form.save() 
     return super(IndexView, self).form_valid(form) 
0

您可以将其添加如下:

class PtoRequestForm(forms.ModelForm): 
    class Meta: 
     # Model to be used in the form. 
     model = PtoHistory 
     # Define which model fields to include in the form. 
     fields = [ 
      'user', 
      'start', 
      'end', 
      'leave_type', 
     ] 
     # Attach input widgets to fields for a friendlier user interface. 
     widgets = { 
      'start': DateTimeWidget(attrs={'id':'start'}, usel10n = True, bootstrap_version=3), 
      'end': DateTimeWidget(attrs={'id':'end'}, usel10n = True, bootstrap_version=3),} 

    def __init__(self, *args, **kwargs): 
     super(PtoRequestForm, self).__init__(*args, **kwargs) 
     self.fields['user'].widget.attrs['readonly'] = True 
+0

我到底在哪里把它放在我的代码中?我已经尝试了几个这样的事情,他们要么不改变任何东西,要么抛出错误。 – FlashBanistan

+0

检查我更新的答案 –

+0

真棒,我们正在取得进展。这样做后,该字段变灰,但我仍然可以选择下拉菜单并更改用户。这可能与'只读'有关吗?是否有另一个属性可以工作,以至于用户甚至无法选择下拉菜单? – FlashBanistan

相关问题