Skip to content

Instantly share code, notes, and snippets.

@fandrefh
Created September 19, 2019 23:45
Show Gist options
  • Save fandrefh/e3e290bf70e90e2e8a0ec8c8592c5d77 to your computer and use it in GitHub Desktop.
Save fandrefh/e3e290bf70e90e2e8a0ec8c8592c5d77 to your computer and use it in GitHub Desktop.
# views.py
def get_pacientes(request):
if request.GET:
q = request.GET.get('term', '')
pacientes = Paciente.objects.filter(nome__icontains = q)[:20]
results = []
for paciente in pacientes:
paciente_json = {}
paciente_json['id'] = paciente.id
paciente_json['label'] = paciente.nome
paciente_json['value'] = paciente.nome
results.append(paciente_json)
data = json.dumps(results)
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, mimetype)
# template
<div class="form-group">
<label for="{{ form_agenda.paciente.id_for_label }}" class="col-sm-2 control-label">Paciente</label>
<div class="col-sm-10">
<input type="type" name="paciente_autocomplete" id="paciente_autocomplete" class="form-control" value="" autofocus placeholder="Campo obrigatório.">
<input type="hidden" name="paciente" id="id_paciente" value=""> <<<<---- aqui
{#{{ form_agenda.paciente|attr:"autofocus"|add_class:"form-control" }}#}
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment