[Answered ]-Implement mapping for MySQL year type in django

2๐Ÿ‘

โœ…

I managed to find following solution which satisfies my needs (models.py):

from django.core.exceptions import FieldError

class YearField(models.Field):
    def db_type(self, connection):
        if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql':
            return 'year'
        else:
            raise FieldError

and then use the column datatype in models:

class Band(models.Model):
    """Music band"""
    class Meta:
        db_table = 'band'
    name = models.CharField(max_length=64)
    active_since = YearField()
    active_until = YearField(null=True)
    genres = models.ManyToManyField(Genre)
    def __unicode__(self):
        return self.name
๐Ÿ‘คducin

Leave a comment