I have two models, one a vanilla Django model, another an unmanaged model wrapping a view.
class A(models.Model):
name = models.CharField(max_length=255)
class B(models.Model):
a = models.ForeignKey(A)
class Meta:
managed = False
db_table = 'myview'
Everything works well, unless I try to delete a row from A, which gives me an error like:
django.db.utils.DatabaseError: cannot delete from view "myview"
HINT: You need an unconditional ON DELETE DO INSTEAD rule or an INSTEAD OF DELETE trigger.
Apparently, Django is assuming the unmanaged model is a table, and is therefore trying to delete the dependent rows, and since it’s a readonly view, my PostgreSQL backend is throwing this error.
Is there any way around this?
I found this bug report outlining the issue, but none of the stopgap hacks seem to work for me, and the bug itself was marked a duplicate of another slightly different yet unresolved bug…
have you tried
on_delete=models.SET_NULLas a workaround?