我创建了一个简单的表单,用户在其中指定(Bloomberg)Ticker的名称。在第二个字段中,用户正在输入网页的地址。当然,她/他可以手动纠正它但我想给他/她一个按钮,以便应用根据第一个Ticker字段的内容建议页面的名称,例如,用户输入" CARMPAT FP Equity"然后应用程序建议正确 https://www.bloomberg.com/quote/CARMPAT:FP
# forms.py
from django.forms import ModelForm, ValidationError
from pybloomberg.bloomberg import valid_ticker
from .models import Symbol
class SymbolAdminForm(ModelForm):
# Have a button next to address field...
class Meta:
model = Symbol
fields = '__all__'
def clean(self):
cleaned_data = self.cleaned_data
ticker = cleaned_data.get('bloomberg_symbol')
return cleaned_data
def react(self):
# extract the content for the bloomberg_symbol field
address = ...
# manipulate the content for the address field
self.... = address
答案 0 :(得分:0)
如果您希望在发布表单后自动应用建议,则可以将逻辑移至clean()方法,并且您需要允许地址字段为空。
def clean(self):
cleaned_data = self.cleaned_data
ticker = cleaned_data.get('bloomberg_symbol')
cleaned_data['address'] = ticker_name_to_address()
return cleaned_data
如果您希望在输入名称时向用户显示建议,您将别无选择,只能使用JavaScript。