Home > Net >  How to parse date string containing mm/yyyy in django models?
How to parse date string containing mm/yyyy in django models?

Time:09-28

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.

CodePudding user response:

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

CodePudding user response:

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')

  • Related