简体   繁体   English

如何选择在 django 表单字段中显示的选项?

[英]How can I choose which choice to show up in django form field?

I am trying to dynamically change the viewable choices in a django form choice field depending on the user.我正在尝试根据用户动态更改 django 表单选择字段中的可见选项。 Here is my form:这是我的表格:

SubmitForm(forms.ModelForm):
    class Meta:
        model = Lesson
        fields = (
            "status",
        )

Here is my models.py for Lesson :这是我的Lesson models.py:

CLASS_STATUS = (
        ('Attended', 'Attended'),
        ('Absent', 'Absent'),
        ('Teacher Postponed', 'Teacher Postponed'),
        ('Student Postponed', 'Student Postponed'),
    )

    status = models.CharField(
        max_length=30,
        choices=CLASS_STATUS,
        null=True,
        blank=True,
    )

So, basically I want to be able to select which choices to show for each type of user.所以,基本上我希望能够为每种类型的用户选择要显示的选项。 In order to do this, I hope you guys could also show how I could access the request.user information in my form.为了做到这一点,我希望你们也可以展示我如何在我的表单中访问request.user信息。 Thank you, and please ask me any questions you have.谢谢,有什么问题可以问我。

Here is a part of the view as some of you requested:以下是你们中一些人要求的部分视图:

class SubmitView(generic.UpdateView):
    template_name = "agents/submit_form.html"
    queryset = Lesson.objects.all()
    context_object_name = "lesson"
    form_class = SubmitForm

I have tried and failed to get this working with a ModelForm.我已经尝试过但未能通过 ModelForm 实现此功能。 I think your best answer is to retreat to using a vanilla Form, and manually update the object.我认为你最好的答案是退回到使用原版表单,并手动更新对象。 Something like就像是

class Status_form( forms.Form):
    status = forms.ChoiceField( choices=Lesson.STATUS_CHOICES)

class UpdateLessonView( FormView):

    form_class = Status_form
    template_name = "agents/submit_form.html"

    def form_valid( self, form):
        self.lesson.status = form.cleaned_data['status']
        self.lesson.save()
        return redirect(
            'lessons:lesson_detail', lesson=self.lesson.pk )

    def get_form( self, form_class=None):
        self.lesson = get_object_or_404( Lesson, id=self.kwargs['lesson'] )
        self.initial = { 'status': self.wafer.status }
        form = super().get_form( form_class)

        # modify choices depending on user
        if self.request.user ... :

            status_choices=(   (..., ...), ... )

            form.fields['status'].choices = status_choices

        return form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用选择字段不显示所有数据,而只显示与 Django 中的选择字段相关的数据? - How to let not all the data show up using a choice field but only the ones related to the choice field in Django? 如何为用户可以选择添加更多字段的表单创建 Django model? - How can I create a Django model for a form in which users can choose to add more fields? 如何在 Django 模型中实现选择字段? - How can I implement choice field in django model? Django中的多选表单字段 - Multi choice form field in Django 如何使表单的选择字段值显示页面上的实际值而不是数字? - How do I make a form's Choice Field values show the actual values on a page instead of numbers? 如何在Django表单选择字段中设置默认值? - How set up default values in Django forms choice field? 如何从列表中选择默认选择字段 - How to choose default choice field from a list 如何格式化Django表单中选择字段中的选择标签? - How to format the labels of choices in a choice field from a Django form? 选择特定选项后,如何更改 Django model 字段的值? - How can I change the value of a Django model field when a particular choice is selected? 使用命令行参数启动时,如何使Django选择特定的默认数据库 - How can I make Django choose a specific default database when it starts up with command line argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM