Django datetimefield as text

Asked

Viewed 1,126 times

0

I am creating an application on Jango where I will have a form to create scheduling queries, my model is 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 a 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'}

Already in my views.py I have the method that will return to 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 %}

And Django generates the form for me, the problem is that the date field of the query instead of showing a field of type datetime shows a field of type text

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

Does anyone know what I need to change so that the field comes out with the correct format? Remembering that in Django admin works normally the way I need.

  • By your code I do not see why the error. Could post your code in gist ?

1 answer

0


By default, the forms.DateTimeField will use widget DateTimeInput that has as input_type ''text. A way to solve this, would be 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

  • It worked! Thanks! I only had to change my code in Jango because HTML brings a T between day and time. but it worked well, thanks!!

  • @Cesarvinicius take a look at this post on how to change the date display. It may also help in this case

Browser other questions tagged

You are not signed in. Login or sign up in order to post.