Before I share my thoughts here is a question for you. Where do you expect 
your product to be printed out? 

Here is what I think you should try out.

First, the source code looks correct to me. Your view only get triggered 
when you access the routes(url) pointing to this view via the browser and 
this doesn't rerun server to print result on your console so you won't see 
anything get printed on the console. I presume you could be thinking of 
running a django app like you do with ordinary python scripts, your right 
but django is a python framework which requires specifics configurations to 
get results.

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

On Friday, April 26, 2024 at 5:22:26 PM UTC+3 Muhammad Juwaini Abdul Rahman 
wrote:

> You're looking for `product`, but the model that you queried is `Task`.
>
> On Fri, 26 Apr 2024 at 17:40, Abdou KARAMBIZI <abdouka...@gmail.com> 
> wrote:
>
>> Hello,
>> *I have 2 models :*
>>
>> class Product(models.Model):
>>     product_id = models.AutoField(primary_key=True)
>>     product_name = models.CharField(max_length=200)
>>     price = models.IntegerField()
>>     image = 
>> models.ImageField(upload_to='images/products_images/',null=True,blank=True)
>>   
>>
>>     def __str__(self):
>>         return self.product_name
>>     
>>
>> class Task(models.Model):
>>     task_id = models.AutoField(primary_key=True)
>>     user = models.ForeignKey(User,on_delete = models.CASCADE)
>>     product = models.ForeignKey(Product,on_delete=models.CASCADE)
>>     performed_at = models.DateTimeField(auto_now_add=True)
>>
>>     def __int__(self):
>>         return self.task_id
>>
>> *and I have following view with queryset  :*
>>
>> def product_task (request):
>> product = Task.objects.select_related().all()
>> for p in product:
>> print(p.product.product_name)
>>
>>
>> *I want to get products don't appear in task model*
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CABnE44ztqCOVMkfDXHMVDAA0b3DpiyuSDKbQw7SNR9ybUvVLhA%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CABnE44ztqCOVMkfDXHMVDAA0b3DpiyuSDKbQw7SNR9ybUvVLhA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4d216a44-5a21-4f3d-aae5-c02553561074n%40googlegroups.com.

Reply via email to