Обновление Django до версии 4.0 Абстрактные модели не могут быть инстанцированы
Я обновляю свое приложение Django с версии 2.2 до версии 4.0.6. Я сделал различные обновления кода, и APP в основном работает.
Но я получаю следующую ошибку "Abstract models cannot be instantiated" и не могу найти подсказки, как решить эту проблему (без понижения версии Django, как в этом посте
).Модели
class AbstractMes(models.Model):
mes = models.DateField(blank=False, null=True)
operaciones = models.IntegerField(blank=True, null=True)
facturacion = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
unidades = models.IntegerField(blank=True, null=True)
diferencia = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
diferencia_porc = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
diferencia_media_movil = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
class Meta:
abstract = True
def __str__(self):
return str(self.mes)
class Meses(models.Model):
mes = models.DateField(blank=False, null=True)
operaciones = models.IntegerField(blank=True, null=True)
facturacion = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
facturacion_dolar = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
unidades = models.IntegerField(blank=True, null=True)
def __str__(self):
return str(self.mes)
Просмотров
meses = Meses.objects.all()
abstract_meses = AbstractMes(meses)
Любые подсказки приветствуются. Спасибо!
Как я знаю, вы не можете создавать экземпляры из абстрактных моделей, вы можете только создавать экземпляры из конкретных моделей, Было бы лучше, если бы вы предоставили старую версию кода
Также в абстракции лучше наследовать конкретную модель от абстрактной модели, например:
class AbstractMes(models.Model):
mes = models.DateField(blank=False, null=True)
operaciones = models.IntegerField(blank=True, null=True)
facturacion = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
unidades = models.IntegerField(blank=True, null=True)
diferencia = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
diferencia_porc = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
diferencia_media_movil = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
class Meta:
abstract = True
def __str__(self):
return str(self.mes)
class Meses(AbstractMes):
facturacion_dolar = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)