Reputation: 1658
I have a custom field to convert the input date to a timedelta in years, rounded up:
class CustomField(models.Model):
start_date = models.DateField()
def days_to_years(self, date: timedelta):
return math.ceil(date.days / 365)
def save(self, *args, **kwargs):
delta = datetime.today() - self.start_date
self.start_date = math.ceil(delta.days / 365)
super(CustomField, self).save(*args, **kwargs)
Which I use in models like:
from django.contrib.postgres.fields import ArrayField
class Component(ComponentBase):
years = ArrayField(
CustomField()
)
When I try to make migrations, the error AttributeError: 'CustomField' object has no attribute 'set_attributes_from_name
is raised, which I can't seem to debug because I'm still quite fresh to Django. Any suggestions would be very welcoming :).
Upvotes: 1
Views: 480
Reputation: 187
If you want your own custom field it should be a subclass of models.Field, not models.Model.
Upvotes: 0