Table editing, Datefield field

Asked

Viewed 2,125 times

1

In one of the Insert’s of my application, I have a field Datefield, At first there is no error when saving the dates in the Database (Sqlite3), i inform a date in format DD/MM/AAAA, and she is saved in the format AAAA/MM/DD at the bank.

When editing some table of the database the fields are already filled with the information already saved with the exception of the date field, no Firefox it is filled in: "July 11, 2014", already on Chrome date field is not filled in with the date I want to edit.

py.models:

class Pessoa(models.Model):

    codigo = models.CharField('Codigo', max_length = 10)
    nome = models.CharField('Nome', max_length = 100)
    d_nasc = models.DateField('Data de Nascimento')
    cpf = models.CharField('CPF',max_length = 14)
    telefone = models.CharField('Telefone',max_length = 12)
    endereco = models.CharField('Endereço',max_length = 200)
    email = models.EmailField('E-mail',max_length = 75)
    tipo = models.CharField('Tipo',max_length = 100)

    biblioteca = models.ForeignKey(Biblioteca, blank=True, null=False)

    def __unicode__(self):
        return self.nome

Forms.py

class FormPessoa (forms.ModelForm):

    class Meta:
        model = Pessoa

py views.

@login_required
def upPessoa(request, id):

    oPessoa = Pessoa.objects.get(pk=id)
    if request.method == 'POST':
        form = FormPessoa(request.POST, instance=oPessoa)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('nPesqUsuario'))
    else:
        form = FormPessoa(instance=oPessoa)

    return render(request,'edicao_usuario.html',
                {
                    'form':form,
                    'codigo':id,
                }
            )

How do I edit the tables this date field is already filled with the date saved in the Database format DD/MM/AAAA?

I don’t want to have to save every field with the field CharField!

The complete project is at Github.

1 answer

3


In the Django 1.7 the Brazilian format is automatically identified, at least that’s what’s happening to me. No Django 1.6 i added the date format on Forms.py, ex:

data_de_nascimento = forms.DateField(
    label=_(u'Data de Nascimento:'), 
    input_formats=["%d/%m/%Y",], 
    widget=forms.DateInput(format='%d/%m/%Y')
)

In the database the saved format will be the default Y-m-d, but in the/Forms template Django will display the format you set.

Browser other questions tagged

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