나는 django CBV에서 form_valid()에서 공통 코드를 제거하려고합니다. 코드는 다음과 같습니다 :상속이 작동하지 않는 것 같습니다
class GenericCreate(CreateView):
def form_valid(self, form):
form.instance.usercreator = self.request.user
form.instance.slug = unique_slug(form.instance.headline, Article)
return super(GenericCreate, self).form_valid(form)
class ArticleCreate(GenericCreate):
success_url = '/'
form_class = ArticleForm
template_name = "articles/article_form.html"
class OrganizationCreate(GenericCreate):
success_url = '/'
form_class = OrganizationForm
template_name = "organizations/organization_form.html"
def form_valid(self, form):
form.instance.pub_date = timezone.now()
# form.instance.usercreator = self.request.user # no error if this is uncommented
return super(GenericCreate, self).form_valid(form)
OrganizationForm가 제출되면, OrganizationCreate 형태로 처리되어 있지만 usercreator는 GenericCreate 클래스에서 설정 얻을하지 않는 것 ... 나는 다음과 같은 오류 얻을 :
NOT NULL constraint failed: articles_article.usercreator_id
을 OrganizationCreate.form_valid()에 form.instance.usercreator = self.request.user
을 강제로 적용하면이 오류가 발생하지 않습니다.
마치 상속이 작동하지 않는 것처럼 보입니다. 어떻게하면 OrganizationCreate.form_valid()에서 createuser를 명시 적으로 설정하지 않고도 GenericCreate.form_valid()를 사용할 수 있습니다.