CreateView를 사용하여 개체를 만든 사용자를 추적하려고하는데 문서에서 수행 한 것과 똑같습니다. (https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-editing/, 모델 및 request.user) 나는 login_required() 데코레이터를 사용하지 않고 대신 django-braces의 LoginRequiredMixin을 사용합니다."owner_id"열의 Django IntegrityError가 null이 아닌 제약을 위반합니다.
내 모델 :
class Contact(models.Model):
owner = models.ForeignKey(User, editable=False)
first_name = models.CharField(max_length=255,)
last_name = models.CharField(max_length=255,)
email = models.EmailField()
내보기 :이 오류가 발생하는 이유는
IntegrityError at /new
null value in column "owner_id" violates not-null constraint
DETAIL: Failing row contains (3, null, John, Smith, [email protected]).
:
class CreateContactView(LoginRequiredMixin, ContactOwnerMixin, CreateWithInlinesView):
model = models.Contact
template_name = 'contacts/edit_contact.html'
form_class = forms.ContactForm
inlines = [forms.ContactAddressFormSet]
def form_valid(self, form):
form.instance.owner = self.request.user
return super(CreateContactView, self).form_valid(form)
나는 새 개체를 만들려고
내가 오류가? 내가하려는 일은 소유자가 생성 될 때 객체에 자동으로 추가된다는 것입니다.... 편집 ...
나는이 문제가 장고 추가 - 뷰에서 해당 CreateWithInlinesView
함께 할 수있는 뭔가가 나타났습니다. 언제 장고의 일반 CreateView
대신 모든 문제없이 작동 내보기를 변경합니다. 그래서 기본적으로 질문은 지금이 솔루션이 CreateWithInlinesView
과 함께 작동하지 않는 이유입니까?