'ForwardManyToOneDescriptor' 对象没有属性 'id' - 错误
'ForwardManyToOneDescriptor' object has no attribute 'id' - Error
我有模特:
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)])
我的看法:
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)
我的url:
path('listselectstktrans/<int:id>/', views.listSelectStkTrans, name='select_stock_report'),
我的模板有 link:
{{ book.code }}
我收到 'ForwardManyToOneDescriptor' 对象没有属性 'id' - 错误。
您没有过滤:
allstktrans = TransBody.objects.filter(<s>id=TransBody.book.id</s>)
因为这里的TransBody
是模型class,所以body
是Field
,这确实有没有 id,你可以过滤:
allstktrans = TransBody.objects.filter(<b>book_id=id</b>)
我有模特:
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)])
我的看法:
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)
我的url:
path('listselectstktrans/<int:id>/', views.listSelectStkTrans, name='select_stock_report'),
我的模板有 link:
{{ book.code }}我收到 'ForwardManyToOneDescriptor' 对象没有属性 'id' - 错误。
您没有过滤:
allstktrans = TransBody.objects.filter(<s>id=TransBody.book.id</s>)
因为这里的TransBody
是模型class,所以body
是Field
,这确实有没有 id,你可以过滤:
allstktrans = TransBody.objects.filter(<b>book_id=id</b>)