Skip to content

Instantly share code, notes, and snippets.

@OHua
Last active August 3, 2018 01:49
Show Gist options
  • Select an option

  • Save OHua/211b21531d9b26db84781e285616ca5b to your computer and use it in GitHub Desktop.

Select an option

Save OHua/211b21531d9b26db84781e285616ca5b to your computer and use it in GitHub Desktop.
error code about content_object in Django Genericforeignkey
FieldError at /member/brand/productmanage/checkarrived/9
Field 'content_object' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.
Request Method: GET
Request URL: http://localhost:8000/member/brand/productmanage/checkarrived/9?ptype=productitem
Django Version: 2.0.6
Exception Type: FieldError
Exception Value:
Field 'content_object' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.
Exception Location: /home/ubuntu/ohua/testenv/lib/python3.5/site-packages/django/db/models/sql/query.py in names_to_path, line 1360
Python Executable: /home/ubuntu/ohua/testenv/bin/python
/home/ubuntu/ohua/gitrepo/pypro/member/views.py in checkarrived
TL.first = ProductPackageClass.objects.filter(package=tl).order_by('date','time').first()
TL.date = TL.first.start_date
TL.time = TL.first.start_time
else :
raise Http404
product = TL.product
orderitems = OrderItem.objects.filter(product=product,content_object=TL) ...
context = {
'TL': TL,
'orderitems': orderitems,
}
return render(request, 'checkarrived.html', context)
▼ Local vars
Variable Value
TL
<ProductItem: 課程a 的課程時間>
id
9
product
<Product: 課程a_huixam@gmail.com>
request
<WSGIRequest: GET '/member/brand/productmanage/checkarrived/9?ptype=productitem'>
@login_required
def checkarrived(request, id):
# if this product is not yours , you should get 404
# TL = Time & Location , whatever productitem or productpackage
if request.GET.get('ptype', False) :
if request.GET.get('ptype') == 'productitem' :
TL = ProductItem.objects.get(id=id)
TL.date = TL.start_date
TL.time = TL.start_time
else :
TL = ProductPackage.objects.get(id=id)
TL.first = ProductPackageClass.objects.filter(package=tl).order_by('date','time').first()
TL.date = TL.first.start_date
TL.time = TL.first.start_time
else :
raise Http404
product = TL.product
orderitems = OrderItem.objects.filter(product=product,content_object=TL)
context = {
'TL': TL,
'orderitems': orderitems,
}
return render(request, 'checkarrived.html', context)
@OHua
Copy link
Copy Markdown
Author

OHua commented Aug 3, 2018

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment