[Python] Using Protocols in django models raises metaclass conflict error

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    Let's say I have PEP-544 protocol called Summable :

    class Summable(Protocol):
    @property
    total_amount()-> Decimal:
    ...



    And I have model Item that implements the Protocol

    class Item(Summable, models.Model):
    discount = models.DecimalField(
    decimal_places=2,
    validators=[MaxValueValidator(1)],
    default=Decimal('0.00'),
    max_digits=10
    )
    price = models.DecimalField(
    decimal_places=4,
    validators=[MinValueValidator(0)],
    max_digits=10
    )

    @property
    def total_amount(self) - > Decimal:
    return self.price - self.price * self.discount

    class Meta:
    ordering = ['id']



    I get:

    TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases


    The same happens even if I extend Item's Meta from Summable.Meta and models.Model.Meta as well.

    I am using python 3.9 Any ideas?

    Continue reading...

Compartilhe esta Página