Ich benutze django-autocomplete-light
in Django 1.8. Aber ich weiß nicht, wie man es in forms.py
verwendet. Anstelle des Autocomplete-Feldes sehe ich ein Auswahlfeld. Ich folgte den Anweisungen von here.Django: Django-Autocomplete-Licht funktioniert nicht richtig
In models.py
ich habe:
class icd_10(models.Model):
id = models.IntegerField(unique=True,primary_key=True,null=False,blank=False)
icd_10_desc = models.CharField('ICD-10 description',max_length=80,null=True,blank=True)
icd_10_code = models.CharField('ICD-10 code',max_length=10,null=True,blank=True)
def __str__(self):
return str(self.icd_10_desc)
class Diagnosis(models.Model):
diagnosis_option = models.ManyToManyField(DiagnosisOption)
record_of_genotype = models.CharField(max_length=45,null=True,blank=True)
icd_10_desc = models.ManyToManyField(icd_10)
patient = models.ForeignKey(Demographic)
def __str__(self):
return str(self.patient)
In settings.py
ich habe:
INSTALLED_APPS = (
'dal',
'dal_select2',
'django.contrib.admin',
...
)
In views.py
ich habe:
class IcdTenAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
#Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return icd_10.objects.none()
qs = icd_10.objects.all()
if self.q:
qs = qs.filter(icd_10_desc__istartswith=self.q)
return qs
In urls.py
ich habe:
url(r'^icd10-autocomplete/$','eReg.views.IcdTenAutocomplete',name='icd10-autocomplete'),
Und in forms.py
ich habe:
class DiagnosisForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DiagnosisForm, self).__init__(*args, **kwargs)
self.helper.layout = Layout(
Fieldset (
# 'patient',
'<b>Diagnosis information</b>',
'diagnosis_option',
'icd_10_desc',
),
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = Diagnosis
exclude = ['patient', 'author']
list_display = ('patient', 'pub_date', 'author')
widgets={'icd10_desc' : autocomplete.ModelSelect2Multiple(url='icd10-autocomplete')}
Wenn ich es direkt vom Browser ausgeführt funktioniert es richtig. Ich bekomme
{"pagination": {"more": false}, "results": [{"text": "Thalassemia", "id": 8}, {"text": "Thalassemia trait", "id": 12}, {"text": "Thalassemia, unspecified", "id": 15}]}
Also, vermisse ich ein richtiges Javascript?