I have the following model:
from django.db import models
from django.db.models import F, Func
from django.db.models.functions import Length
from django_reuse.abstract_models import Timestamped
class Painting(Timestamped):
work_name = models.CharField(max_length=60)
work_name_length = models.GeneratedField(
# expression=Func(F('work_name'), function='Length'),
expression=Length(F('work_name')),
output_field=models.PositiveSmallIntegerField(),
db_persist=False
)
Everything seems to work okay, but when I open my Sqlite database, I see the generated field was persisted to the database anyway:
Not sure if I am doing something wrong, or if it is a bug, but if a column will be created in the database anyway, I would just use an IntegerField directly and do the calculation via overriding the save method.
I seem to get equivalent results, so is there any advantage to still using the newer field which seems to be broken, or perhaps just broken specfically with Sqlite?
from django.db import models
from django.db.models import F # Func
from django.db.models.functions import Length
from django_reuse.abstract_models import Timestamped
class Painting(Timestamped):
title = models.CharField(max_length=60)
title_len = models.PositiveSmallIntegerField(blank=True)
title_len_gen = models.GeneratedField(
# expression=Func(F('title'), function='Length'),
expression=Length(F('title')),
output_field=models.PositiveSmallIntegerField(),
db_persist=False
)
class Meta:
ordering=['title']
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.title_len = len(self.title)
super(Painting, self).save(*args, **kwargs)