0
선택 옵션 속성을 사용자 정의해야하는 사용자 정의 요구 사항에 대해 작업하고 있습니다. 내 드롭 다운 목록에 옵션 목록이 표시되며 일부 옵션은 특정 조건에 따라 사용 중지됨으로 표시되어야합니다.forms의 속성을 사용자 정의하는 방법 장고에서 옵션을 선택하십시오.
나는 Select widget attributes
disabled = disabled
를 전달하여 사용자 정의 노력했다. 하지만 이렇게하면 전체 드롭 다운이 비활성화됩니다. 장고 1.11.5의 코드를 살펴보면 Select에 적용된 속성이 해당 옵션에 적용되는 것으로 보입니다.
누구든지이 기능을 어떻게 달성 할 수 있습니까? 이 도움이
class MySelect(Select):
def __init__(self, attrs=None, choices=(), disabled_choices=()):
super(Select, self).__init__(attrs, choices=choices)
# disabled_choices is a list of disabled values
self.disabled_choices = disabled_choices
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
option = super(Select, self).create_option(name, value, label, selected, index, subindex, attrs)
if value in disabled_choices:
option['attrs']['disabled'] = True
return option
희망 :
난 당신의__init__
기능에 새 매개 변수
disabled_choices
을 통과 서브 클래스
django.forms.widgets.Select
위젯을 수 있다고 생각이 같은
create_option
메소드를 오버라이드 (override) 당신에게
정말 고마워요. 정말 큰 도움이됩니다. 나는 사소한 변경을 한 후에 그것을 작동시킬 수 있었다 :-) –