Q:

How do I set django generic foreign key from my views?

I have 4 forms that the user submits by clicking one Submit button. The forms are:

Person Form - Will Save to Person Model
Address Form - Will Save to Address Model
Email Form - Will save to Email Model
Phone Form - Will save to Phone Model. 

So, a Person can have multiple Addresses, Emails and Phone Numbers. So, I did this in the Person Model:
class Person(models.Model):
    first_name = models.CharField(max_length=99)
    middle_name = models.CharField(max_length=99, blank=True, null=True)
    last_name = models.CharField(max_length=99, blank=True, null=True)
    address = GenericRelation('Address')
    phone = GenericRelation('Phone')
    email = GenericRelation('Email')

I have these 3 lines along with their respective model fields for Address, Phone and Email
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')

Now, how do I save data to these models properly in my views when I receive the form from the user? This what I have so far.
 if request.method == 'POST':
        if person_form.is_valid() and address_form.is_valid() and email_form.is_valid() \
                and phone_form.is_valid():

            address = address_form.save()
            email = email_form.save()
            phone = phone_form.save()

            person = person_form.save(commit=False) #This person model has a generic foreign key relation with Address, Email and Phone
            person.address = (address)  #This is where I need help. Am I thinking right? Is this the right way to save?

A:

You need to set the content object in model where GFK is defined,  I have modified the code,
 if request.method == 'POST':
                if person_form.is_valid() and address_form.is_valid() and email_form.is_valid() \
                        and phone_form.is_valid():

                person = person_form.save()

                address = address_form.save(commit=False)
                address.content_object = person
                address.save()
                email = email_form.save(commit=False)
                email.content_object = person
                email.save()
                phone = phone_form.save(commit=False)
                phone.content_object = person
                phone.save()

But i have one serious doubt ? why are your using Generic Foreign Key in your models ? Can these models refer to any other model other than Person. 

