0

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.

3 Answers 3

3

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.

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

Comments

2

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).

Comments

1

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().

1 Comment

request.user won't be AnonymousUser, because the login_required decorator ensures that the current user is a valid user and authenticated.

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.