ContentType 일치 쿼리가 post_syncdb에 없습니다
-
12-12-2019 - |
문제
PeneraCodiceTag 코드 신호를 사용하여 테이블이 생성되는 즉시 데이터베이스에 추가 데이터를 추가하려고합니다.
signals.post_syncdb.connect(init)
.
그런 다음 init 함수에서 권한을 설정하고 싶습니다. 그래서 를 사용합니다.
ct = ContentType.objects.get(app_label='news', model='Article')
Permission(name='Approve articles', codename='can_approve_article', content_type=ct)
.
그러나 모든 테이블을 삭제하고 post_syncdb
를 실행하면 를 얻습니다.
...
File "...\base\functions\init.py", line 11, in init
ct = ContentType.objects.get(app_label='news', model='Article')
...
django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.
.
내가 한 일부 테스트 :
-
syncdb
외부 에서이 코드를 사용해 보면 잘 작동합니다. -
syncdb
가이 코드가없는 모든 테이블을 만들고이 코드를 추가하고 변경해야하지 않고 syncdb를 실행하면 - 와 나는 그것이 일하기 위해 익숙해 져 있지만, 그 이후로 다른 곳에서 많은 것을 바꿨다. 그래서 나는 어디에서 시작 해야할지 모른다.
- 다른 앱의 다른 모델에 동일한 오류가 발생합니다.
- 신호는 약 10 배로 발생하며, 처음 몇 번만 오류를 던지십시오.
어떤 힌트에 대해 많은 감사합니다!
해결책
이 init 기능의 시작 부분에 이것을 추가합니다.
from django.contrib.contenttypes.management import update_all_contenttypes
update_all_contenttypes() # make sure all content types exist
. 제휴하지 않습니다 StackOverflow