I am having model:
class TextBook(models.Model):
code = models.CharField(max_length=200, unique=True)
type = models.CharField(max_length=200)
name = models.CharField(max_length=200)
open_qty = models.DecimalField( max_digits = 4, decimal_places = 0,default = 0)
recived_qty =models.DecimalField( max_digits = 4, decimal_places = 0,default = 0)
return_qty=models.DecimalField( max_digits = 4, decimal_places = 0,default = 0)
issue_qty=models.DecimalField( max_digits = 4, decimal_places = 0,default = 0)
bal_qty = models.DecimalField( max_digits = 4, decimal_places = 0,default = 0)
avgcost =models.DecimalField( max_digits = 5, decimal_places = 2,default = 0.00)
price =models.DecimalField( max_digits = 5, decimal_places =2,default = 0.00)
class_name = models.ManyToManyField(SchClass, help_text='Select a class for this book')
term = models.ManyToManyField(SchTerms, help_text='Select Terms')
class TransBody(models.Model):
trans_header = models.ForeignKey(TransHeader,on_delete=models.CASCADE,null=True)
book = models.ForeignKey(TextBook,on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
price = models.FloatField(default=0)
def get_absolute_url(self):
"""Returns the url to access a detail record for this book."""
return reverse('select_stock_report', args=[str(self.id)])
my view:
def listSelectStkTrans(request,id):
# (note:return(HttpResponse(id) gives correct book id)
allstktrans =TransBody.objects.filter(id=TransBody.book.id)
context = {'allstktrans': allstktrans}
return render(request, 'wstore/list_stk_trans.html', context)
my url:
path('listselectstktrans/<int:id>/', views.listSelectStkTrans, name='select_stock_report'),
my template has link:
{{ book.code }}I am getting 'ForwardManyToOneDescriptor' object has no attribute 'id' - Error.
You do not filter with:
allstktrans = TransBody.objects.filter(id=TransBody.book.id)
since a TransBody
here is a model class, hence body
is a Field
, and this indeed has no id, you can filter with:
allstktrans = TransBody.objects.filter(book_id=id)