django – TypeError how to get instance of a foreignkey from the user instance

Question:

I am getting the following type error and its due to this instance in view. The model as you can see below where Employee is onetoone relation to User and Company is the foreignkey to the Employee. How would I be able to solve this by getting the instance of company ? Or what is the problem.

This is the problem:

companysetting_form = CompanySettingEdit(instance = request.user.employee.company)

Below is the typeerror

TypeError at /employee/companysettings/    
__init__() got an unexpected keyword argument 'instance'

Request Method: GET
Request URL:    http://127.0.0.1:8000/employee/companysettings/
Django Version: 1.11.7
Exception Type: TypeError
Exception Value:    
__init__() got an unexpected keyword argument 'instance'
Exception Location: /project/railercom/railercomapp/views.py in employee_companysettings, line 83
Python Executable:  /project/myvirtualenv/railercom/bin/python
Python Version: 3.6.3
Python Path:    
['/project/railercom',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
 '/project/myvirtualenv/railercom/lib/python3.6/site-packages']

Below are my code:

@login_required(login_url='/employee/sign-in/')
def employee_companysettings(request):

companysetting_form = CompanySettingEdit(instance = request.user.employee.company)  <---- this is the problem

if request.method == "POST":
    companysetting_form = CompanySettingEdit(request.POST, instance = request.user.employee.company)

    if companysetting_form.is_valid():
        companysetting_form.save()

return render(request, 'employee/account.html', {
    "companysetting_form":companysetting_form
    })



class CompanySettingEdit(forms.Form):
    name = forms.CharField(max_length=50, required=False)
    tel = forms.CharField(max_length=50, required=False)
    address_1 = forms.CharField(max_length=50, required=False)
    address_2 = forms.CharField(max_length=50, required=False)
    address_zip = forms.CharField(max_length=50, required=False)
    address_city = forms.CharField(max_length=50, required=False)
    address_state = forms.CharField(max_length=50, required=False)
    address_country = forms.CharField(max_length=50, required=False)

    class Meta:
        model = Company
        fields = ("name", "tel", "address_1", "address_2", "address_zip",
                  "address_city","address_state","address_country")



      <form method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        {% bootstrap_form companysetting_form %}
        <button type="submit" class="btn btn-pink">Update</button>
      </form>



class Company(models.Model):
    name = models.CharField(max_length=50)
    tel = models.CharField(max_length=15, blank=True)
    address_1 = models.CharField(max_length=50, blank=True)
    address_2 = models.CharField(max_length=50, blank=True)
    address_zip = models.CharField(max_length=20, blank=True)
    address_city = models.CharField(max_length=20, blank=True)
    address_state = models.CharField(max_length=20, blank=True)
    address_country = models.CharField(max_length=20, blank=True)

    logo = models.ImageField(upload_to='company_logo/', blank=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name
Asked By: Axil

||

Answers:

This question doesn’t have anything to do with foreign keys.

Plain Form subclasses don’t know anything about models, ignore the inner Meta class, don’t have a save method, and don’t accept an instance argument.

To enable all these things, your form needs to subclass forms.ModelForm.

As a bonus, once you’ve done that you can remove all the field definitions in the class itself, as they will be taken from the model definition.

Answered By: Daniel Roseman