python - How to pass a value in django select form? -
i creating invoice application. have model receivables as
class receivables(models.model): user=models.foreignkey(settings.auth_user_model) receivable_name=models.charfield(max_length=250) receivable_address=models.charfield(max_length=250,blank=true) receivable_email=models.charfield(max_length=250,blank=true) receivable_telephone=models.charfield(max_length=250,blank=true) receivable_vat=models.charfield(max_length=250,blank=true) receivable_service_tax=models.charfield(max_length=250,blank=true) receivable_pan=models.charfield(max_length=250,blank=true) def __str__(self): return self.user.receivable_name
have model serviceinvoice as
class serviceinvoice(models.model): user=models.foreignkey(user,related_name='invoice') invoice_number=models.positiveintegerfield() invoice_date = models.datefield(auto_now_add=true) invoice_receivable=models.foreignkey(receivables) total_amount=models.decimalfield(decimal_places=2,max_digits=20) total_amountwithtax=models.decimalfield(decimal_places=2,max_digits=20) def __str__(self): return invoice_number
form.py serviceinvoice
model as
class createinvoiceform(forms.modelform): class meta: model=serviceinvoice exclude=('user',)
views.py form as
@login_required def createinvoice(request): if request.method == 'post': inv_form=createinvoiceform(data=request.post) if inv_form.is_valid(): new_form=inv_form.save(commit=false) new_form.user=request.user new_form.save() return httpresponse('invoice created') else: inv_form=createinvoiceform() return render(request,'account/createinvoice.html',{'inv_form':inv_form})
now issues facing are:
- rendered template not show receivalbe name instead shows
self.user.receivalbe_name
- how show receivalbe_name related logged in user?
second issue solved putting following in views.py:
inv_form=createinvoiceform() inv_form.fields["invoice_receivable"].queryset=receivables.objects.filter(user=request.user)
Comments
Post a Comment