To test your code and see products get printed out try this:
1. Use a template. Modify your view as follows:
def product_task(request):
products = Task.objects.select_related().all()
context = {'products': products}
return render(request, 'product-task.html', context)
Create the template, configure routes in urls.py to point to this view and access through browser. Remember to add dummy data in your db
2. To print out your products using a for loop, use the django shell using 'python manage.py shell'
e.g.
(.venv)
python manage.py shell
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from stores.models import Task, Product
>>> products = Task.objects.select_related().all()
>>> for p in products:
... print(p.product.product_name)
...
Coffee
Banana Bread
Note:
1. Stores refers to a dummy app I have created to host models.py.
2. Coffee and Banana Bread are just products I have added as dummy data.
Hopes this help or atleast gives a guide somehow