0

I'm writing existing SQLite3 database to django models. Some dates replicate themselves and I need them encoded, so I create a class in the model to store unique periods in the following form: mm/yyyy.

What would be the right syntax to set date, so in django they have the same format, but as a datetime object?

class Periods(models.Model):
    period = models.DateField()

    def __str__(self):
        return self.period

Thanks in advance.

Brian Destura
  • 11,487
  • 3
  • 18
  • 34
mieltn
  • 29
  • 5
  • Have you tried this ? https://stackoverflow.com/a/30911955/15042684 – Thierno Amadou Sow Sep 28 '21 at 08:26
  • input_formats seems to be an argument for forms.DateField() not models.DateField(), so it throws an error `TypeError: __init__() got an unexpected keyword argument 'input_formats'` Is there any chance I can use something similar for models.DateField()? – mieltn Sep 28 '21 at 08:46

2 Answers2

2
def __str__(self):
    return self.period.strftime('%m/%Y')
0

Try to store the whole date in period field and in your str print only the mm/yyyy.

Like this.

def __str__(self): 
    return self.period.strftime('%m/%Y')
EL hassane
  • 134
  • 1
  • 9