我使用了PurchaseInfo.objects.filter(user=request.user).values()
,并希望在模板中显示结果。但是,我收到此错误消息:
FieldError at /auth/purchaseHistory/
Cannot resolve keyword 'user' into field. Choices are: id, product_name, product_price, purchase_addr, purchase_date, purchase_id, purchase_id_id, purchase_name, purchase_phone
purchaseinfo / models.py
class PurchaseInfo(models.Model):
purchase_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
purchase_name = models.CharField(max_length=30)
purchase_addr = models.CharField(max_length=100)
purchase_phone = models.CharField(max_length=15)
...and so on
customlogin / views.py
from purchaseinfo.models import PurchaseInfo
def purchaseHistory(request):
history = PurchaseInfo.objects.filter(user=request.user).values()
return render(request,'customlogin/purchaseHistory.html',{'history':history})
purchaseHistory.html
{% for i in history %}
<tr>
<td>{{i.purchase_id}}</td>
<td>{{i.purchase_name}}</td>
<td>{{i.purchase_addr}}</td>
<td>{{i.purchase_phone}}</td>
<td>{{i.product_name}}</td>
<td>{{i.product_price}}</td>
<td>{{i.purchase_date}}</td>
</tr>
{% endfor %}
我该如何解决这个问题?
答案 0 :(得分:3)
对此进行更新:
history = PurchaseInfo.objects.filter(purchase_id=request.user).values()
在模型中,与用户模型的外键关系为purchase_id
,但是在视图中,您尝试按user
进行过滤。您需要按purchase_id
进行过滤。
大多数情况下,错误消息会很好地说明问题,请尝试理解问题的根源并尝试自行解决。调试是一项不错的技能。