I already tried nearly everything I could find regarding this, but I am pretty sure I am just one small suggestion away from solving my issue.
I am trying to save to forms that I generated using the forms method of Django at the same time. These forms have a ForeignKey relationship.
My model:
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')
and my view:
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/')
So, what I try to do, is to save the second form "formQuestions" with the foreign key publisher against the publisher_id from the PublisherForm.
Unfortunately MySQL / Django is giving me the following error.
Column 'publisher_id' cannot be null
This might be a complete newbie question and yes there has been many people asking nearly the same, but none of the solutions worked for me.
Thanks for your help, as always appreciated!