CA Pulkit Sharma
CA Pulkit Sharma

Reputation: 41

Retrieving data while rendering form in django

I have a form with some fields. I want to display all fields in form except two. However one field data needs to be displayed. I am able to get the form but not able to retrieve the data from DB to display.

Model.py

class Company(models.Model):
    STATUS_CHOICES=(
                    ('service','service'),
                    ('product','product'),
    )
    user=models.OneToOneField(settings.AUTH_USER_MODEL)
    company_name=models.CharField(max_length=250)
    company_address=models.CharField(max_length=250)
    Company_telephone=models.CharField(max_length=250,blank=True)
    company_email=models.CharField(max_length=250,blank=True)
    company_website=models.CharField(max_length=250,blank=True)
    VAT=models.CharField(max_length=250,blank=True)
    Service_Tax=models.CharField(max_length=250,blank=True)
    company_PAN=models.CharField(max_length=250,blank=True)
    company_bankdetails=models.CharField(max_length=250,blank=True)
    invoice_type=models.CharField(max_length=250,choices=STATUS_CHOICES,default='service')

    def __str__(self):
        return 'self.user.company_name'

forms.py

class companyeditform(forms.ModelForm):
    class Meta:
        model=Company
        exclude = ('user','company_name',)

views.py

@login_required
def companyadd(request):
    if request.method == 'POST':
        company_form=companyeditform(instance=request.user.company,data=request.POST)
        if company_form.is_valid():
            new_form=company_form.save(commit=False)
            new_form.save()
            return render(request,'account/dashboard.html',{'section':'addcompany'})
    else:
        company_form=companyeditform(instance=request.user.company)
    company_details=Company.objects.get(user=request.user.company)
    return render(request,'account/company.html',{'company_form':company_form})

When form is displayed everything works as planned. However not getting company_name. Using this query to get company name.

company_details=Company.objects.get(user=request.user.company)

Django gives following error:

Cannot query "self.user.company_name": Must be "User" instance.

Upvotes: 0

Views: 141

Answers (3)

Ashique PS
Ashique PS

Reputation: 691

In this query company_details=Company.objects.get(user=request.user.company) you are trying to get the company of a particular user. But in the statement, you are comparing user=request.user.company, both are two different types (User is the authuser model and request.user.company is the Company model). You cannot do this in the query.

company_details=Company.objects.get(user=request.user) This statement will solve the issue. And also you can do company_details=request.user.company because the association is OneToOne.

Upvotes: 3

Burhan Khalid
Burhan Khalid

Reputation: 174614

The reason you are getting that error, is because you are trying to fetch a Company by filtering it out the company that matches the current user, but you are passing in the actual company object:

company_details=Company.objects.get(user=request.user.company)
#                                                     ^^^^^^^

You can fix the line, by doing this:

company_details=Company.objects.get(user=request.user)

But you already have the correct object, in request.user.company, you don't need to fetch it again, simply:

company_details = request.user.company
print(company_details.company_name)

In fact, since you are using the render shortcut, you don't even need to do this step, as the request object will be available in your template, so all you need to do really is:

Company: {{ request.user.company.company_name }}

Finally, you should always redirect after a POST request (see this article on wikipedia for the details).

Upvotes: 2

contracode
contracode

Reputation: 405

request.user might be a class like AnonymousUser. Do some extra processing to ensure that request.user is the type provided by django.contrib.auth.get_user_model().

Upvotes: 1

Related Questions