1

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!

1 Answer 1

1

You can try the following to check whether you get the same error or not:

publisher = form.save()
questions = formQuestions.save(commit=False)
questions.publisher = publisher
questions.save()

With commit=False you get the Model without saving it to the database. https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

In fact it is the Model you want to manipulate, adding the Publisher and not the form.

If you need to access the data from the form I think you should use for example this: https://docs.djangoproject.com/en/dev/ref/forms/api/#accessing-clean-data

How I understood it is that you have a ModelForm and not a Model, so if you want to manipulate the Model you first need to create it, or at least it is the cleanest way of manipulating Model data. Otherwise you can manipulate the Form data but it is another story, you want to choose the raw data or the clean data etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.