'Create a python object with a date field 3 months from now [duplicate]
I'm learning python, hence this is probably an obvious solution for some, but I am trying to create a class that, whenever created, has a test field with a date value of three months from now.
My class is as follows:
class Something(models.Model):
test = models.DateTimeField(default=self.get_date())
def get_date(self):
today = datetime.date.today()
return today + relativedelta(months=1)
I thought that I could define it as a function and then call it to set the date. Can someone advise what's wrong here? Any links to relevant documentation would also be appreciated, thank you.
Solution 1:[1]
If you want a subclass to have a specific attribute, like the "test field" you could define it in the parents class like so:
import models
class Class:
test = models.DateTimeField()
class Subclass(Class)
With this solution every "Sublass-Object" would have the attribute "test" in it when initialized.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | W4tu |
