2013-01-24 2 views
6

내 mysql 데이터베이스에서 뷰를 나타내는 models.Model 하위 클래스가 있습니다 (예 : managed = False). 내 단위 테스트를 실행할 때django 모델 인스턴스 삭제 방지

그러나, 나는 얻을이 삭제 요청

DatabaseError: (1288, 'The target table my_view_table of the DELETE is not updatable')

소스는 외부 키를 통해 (간접적으로)입니다. 내 테스트, 나는 ActualTableModel 인스턴스를 삭제하고의의 tearDown 동안

class MyViewModel(models.Model): 
    problematic_field = models.ForeignKey(ActualTableModel) # specifying on_delete=models.SET_NULL simply replaces the DELETE error with an UPDATE one 

, 그것은 문서화 된 행동 다음되는 장고를 나타납니다 : 나는 (간체) 한

When Django deletes an object, it emulates the behavior of the SQL constraint ON DELETE CASCADE -- in other words, any objects which had foreign keys pointing at the object to be deleted will be deleted along with it.

이 야기 된 것으로 판단 문제가 (관리 = 거짓)보기에 적용됩니다.

내가 삭제를 방지하기 위해 삭제 방법을 재정의하는 시도 :

class MyViewModel(models.Model): 
    ... 
    def delete(self, *args, **kwargs): 
     pass # deletion of a view row is never valid 

을하지만 문제가 해결되지 않았다.

이 동작을 어떻게 막을 수 있습니까?

답변

7

아마도 다양한 on_delete argument options 중 하나를 시도해 볼 수 있을까요?

즉 : 문서 링크에 대한

problematic_field = models.ForeignKey(ActualTableModel, on_delete=models.PROTECT) 
+1

감사합니다. 나는 on_delete = models.SET_NULL을 시도했지만 DO_NOTHING을 간과했다. 후자는 오류를 수정합니다. – sapi