Django datetimefield as text

0

I am creating an application in django where I will have a form for creating a query schedule, my model looks like this:

class Agenda(models.Model):
"""Criação da agenda de consultas da Clinica"""
data_consulta = models.DateTimeField()
paciente = models.ForeignKey(Paciente, on_delete=models.CASCADE)
servico = models.ForeignKey(Servico, on_delete=models.DO_NOTHING)
observacoes = models.TextField()
status = models.ForeignKey(StatusConsulta, null=True, blank=True, on_delete=models.DO_NOTHING)
form_pagamento = models.ForeignKey(FormPagamento, null=True, blank=True, on_delete=models.DO_NOTHING)
consulta_realizada = models.BooleanField()
data_criacao = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return self.servico.servico

class Meta:
    verbose_name = "Agenda"

I created an AgendaForm class to generate the form:

class AgendaForm(forms.ModelForm):
class Meta:
    model = Agenda
    fields = ['paciente', 'servico', 'data_consulta', 'observacoes', 'status', 'form_pagamento']
    labels = {'paciente': 'Paciente', 'servico': 'Serviço',
              'observacoes': 'Observações', 'status': 'Status', 'form_pagamento': 'Forma de Pagamento'}

In my views.py already have the method that will return my view:

def novoAgendamento(request):
"""Cadastra novos agendamentos"""
if request.method != 'POST':
    form = AgendaForm
    context = {'form': form}
    return render(request, 'core/cadastraConsulta.html', context)

and finally my view:

       {% extends 'core/base.html' %}
{% block content %}
<h1>Agenda Consulta</h1>
<form action="{% url 'core:novoAgendamento' %}" method="post">
    <table>
        {% csrf_token %}
        {{ form.as_table }}
    </table>
    <button>Salvar</button>
</form>

{% endblock content %}

<input type="text" name="data_consulta" id="id_data_consulta" required />

Does anyone know what I need to change in order for the field to come out in the correct format? Remembering that in django admin it works normally the way I need it.

    
asked by anonymous 18.07.2018 / 16:49

1 answer

0

By default, forms.DateTimeField will use the DateTimeInput which has as input_type 'text'. One way to resolve this would look like this:

class DateInput(forms.DateInput):
    input_type = 'date'

class AgendaForm(forms.ModelForm):
    class Meta:
        model = Agenda
        widgets = {
            'data_consulta': DateInput()
        }

Reference to the above code

    
19.07.2018 / 17:59