Retrieving data while rendering form in django -
i have form fields. want display fields in form except two. 1 field data needs displayed. able form not able retrieve data db 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 displayed works planned. not getting company_name. using query company name.
company_details=company.objects.get(user=request.user.company)
django gives following error:
cannot query "self.user.company_name": must "user" instance.
in query company_details=company.objects.get(user=request.user.company)
trying company of particular user. in statement, comparing user=request.user.company
, both 2 different types (user authuser model , request.user.company company model). cannot in query.
company_details=company.objects.get(user=request.user)
statement solve issue. , can company_details=request.user.company
because association onetoone.
Comments
Post a Comment