django-autocomplete-light 与脆形式
django-autocomplete-light with crispy forms
我正在尝试在我的表单中的外键字段上实现自动完成搜索。我已经阅读了文档,但不确定我需要做什么才能使其正常工作。我只是在外键字段上得到了正常的下拉框。
这是我的尝试:
settings.py
INSTALLED_APPS = (
'crispy_forms',
'autocomplete_light',
)
urls.py
url(r'^autocomplete/', include('autocomplete_light.urls')),
models.py
class Client(models.Model):
...
class Branch(models.Model):
client = models.ForeignKey(Client, related_name='clients')
...
forms.py
import autocomplete_light
class BranchForm(autocomplete_light.ModelForm):
class Meta:
model = Branch
autocomplete_fields = ('client')
exclude = ('creation', 'last_modified')
form.html
<form method="POST" action="">{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Submit" />
<a href="{% url 'branch' %}"><button type="button" class="btn btn-danger">Cancel</button></a>
</form>
最后想通了。需要注册它。
autocomplete_light.register(Branch,
name = 'ClientAutocomplete',
choices = Client.objects.all()
)
class ClientForm(forms.ModelForm):
class Meta:
model = Client
exclude = ('creation', 'last_modified')
class BranchForm(autocomplete_light.ModelForm):
class Meta:
model = Branch
autocomplete_names = {'client':'ClientAutocomplete'}
exclude = ('creation', 'last_modified')
我正在尝试在我的表单中的外键字段上实现自动完成搜索。我已经阅读了文档,但不确定我需要做什么才能使其正常工作。我只是在外键字段上得到了正常的下拉框。
这是我的尝试:
settings.py
INSTALLED_APPS = (
'crispy_forms',
'autocomplete_light',
)
urls.py
url(r'^autocomplete/', include('autocomplete_light.urls')),
models.py
class Client(models.Model):
...
class Branch(models.Model):
client = models.ForeignKey(Client, related_name='clients')
...
forms.py
import autocomplete_light
class BranchForm(autocomplete_light.ModelForm):
class Meta:
model = Branch
autocomplete_fields = ('client')
exclude = ('creation', 'last_modified')
form.html
<form method="POST" action="">{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Submit" />
<a href="{% url 'branch' %}"><button type="button" class="btn btn-danger">Cancel</button></a>
</form>
最后想通了。需要注册它。
autocomplete_light.register(Branch,
name = 'ClientAutocomplete',
choices = Client.objects.all()
)
class ClientForm(forms.ModelForm):
class Meta:
model = Client
exclude = ('creation', 'last_modified')
class BranchForm(autocomplete_light.ModelForm):
class Meta:
model = Branch
autocomplete_names = {'client':'ClientAutocomplete'}
exclude = ('creation', 'last_modified')