I have the following two models:
class Timestampable(models.Model):
created_at = models.DateTimeField(null=True, default=None)
updated_at = models.DateTimeField(null=True, default=None)
class Meta:
abstract = True
def save(self, *args, **kwargs):
now = timezone.now()
if not self.created_at:
self.created_at = now
self.updated_at = now
super(Timestampable, self).save(*args, **kwargs)
class Attachment(Timestampable, models.Model):
uuid = models.CharField(max_length=64, unique=True)
customer = models.CharField(max_length=64)
user = models.CharField(max_length=64)
file = models.FileField(upload_to=upload_to)
filename = models.CharField(max_length=255)
mime = models.CharField(max_length=255)
publicly_accessible = models.BooleanField(default=False)
When I try to migrate these models, I get the following error:
django.core.exceptions.FieldError: Local field 'created_at' in class 'Attachment' clashes with field of similar name from base class 'Timestampable'
I read here, here, and here that this should work when the base class is abstract. However, I marked it as abstract it still it doesn't seem to work. What else could be wrong? I am using Django 1.8.14.
I found what was the problem. I used to have the class Timestampable
not inherit from models.Model
. Therefore, in one of my initial migrations, I had the line:
bases=(at_common.behaviours.Timestampable, models.Model),
I was looking for a way to remove this. Turned out that I just had to delete this line from the initial migration file.