Skip to content

Exception on delete of polymorphic collection #357

@unusual-thoughts

Description

@unusual-thoughts
class Order(models.Model):
    title = models.CharField(_("Title"), max_length=200)

    class Meta:
        ordering = ('title',)

    def __str__(self):
        return self.title

class Payment(PolymorphicModel):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    amount = models.DecimalField(default=0, blank=True, max_digits=10, decimal_places=2)
    index = models.PositiveIntegerField(default=0, blank=False)

    class Meta:
        ordering = ('index',)

class CreditCardPayment(Payment):
    card_type = models.CharField()

class Beneficiary(models.Model):
    firstname = models.CharField()
    lastname = models.CharField()

class SepaPayment(Payment):
    iban = models.CharField()
    bic = models.CharField()
    beneficiaries = models.ManyToManyField(Beneficiary, "sepa", blank=True)

When deleting an Order whose first Payment is of type SepaPayment, an exception is raised :
Exception Value: Cannot query <CreditCardPayment>: Must be "SepaPayment" instance.

This is because of this code in django core: https://github.com/django/django/blob/master/django/db/models/deletion.py#L193

The model type is taken from the first element in the polymorphic collection

Minimal example here: https://github.com/unusual-thoughts/django-poly-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions