나는 이것에 관해서 내가 발견 할 수있는 거의 모든 것을 시도했지만, 나는 내 문제를 해결할 때 한 가지 제안 만하고있다.Django multi-form 저장
Django의 forms 메서드를 사용하여 동시에 생성 한 양식에 저장하려고합니다. 이 양식에는 ForeignKey 관계가 있습니다.
내 모델 :
class Publisher(models.Model):
company = models.CharField(max_length=255)
address1 = models.CharField(max_length=255)
address2 = models.CharField(max_length=255)
city = models.CharField(max_length=255)
zipcode = models.CharField(max_length=10)
email = models.EmailField(max_length=255)
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
tc = models.BooleanField()
timestamp = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.company
class PublisherQuestions(models.Model):
referal = models.TextField()
updates = models.BooleanField()
publisher = models.ForeignKey(Publisher)
preferredCommunication = models.ForeignKey(PublisherCommunication)
def __unicode__(self):
return self.publisher
class PublisherForm(ModelForm):
class Meta:
model = Publisher
class PublisherQuestionsForm(ModelForm):
class Meta:
model = PublisherQuestions
exclude = ('publisher')
내보기 :
def register(request):
if request.method == 'POST':
form = PublisherForm(data = request.POST)
formQuestions = PublisherQuestionsForm(data = request.POST)
if form.is_valid() and formQuestions.is_valid():
publisher = form.save()
formQuestions.publisher = publisher
formQuestions.save()
return HttpResponseRedirect('/thanks/')
그래서, 내가하려고 무엇의 PUBLISHER_ID에 대한 외래 키 게시자와 두 번째 양식 "formQuestions"를 저장하는 것입니다 PublisherForm에서.
불행히도 MySQL/Django에서 다음과 같은 오류가 발생합니다.
Column 'publisher_id' cannot be null
이
은 완전한 초보자 질문 할 수 있으며 예 거의 같은 요구 많은 사람들이 있었다,하지만 솔루션 중 누구도 나를 위해 일하지 않는다.언제나 감사드립니다.