Re: Displaying contrast of a queryset

2024-05-01 Thread Kelvin Macharia
Actually:
Query for tasks without relations to Product

tasks = Task.objects.filter(product__isnull=True)

after setting product field optional in as follows:

product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True, 
blank=True)

On Wednesday, May 1, 2024 at 4:59:46 PM UTC+3 Ryan Nowakowski wrote:

> Products.objects.filter(task_set__isnull=True)
>
>
> On April 28, 2024 8:57:57 AM CDT, manohar chundru <
> chundrumanoh...@gmail.com> wrote:
>
>> print(p)
>>
>> On Sun, Apr 28, 2024 at 12:25 AM Abdou KARAMBIZI  
>> wrote:
>>
>>> Hello friends,
>>>
>>> products = Task.objects.select_related().all()
>>> for p in products:  
>>> print(p.product.product_name)
>>>
>>> This gives product that has relation in Task model but *I need product 
>>> which doesn't have relation in Task *
>>>
>>> *Means we have products have relations in Task model and others with no 
>>> relation in Task model and I need a queryset to display those with no 
>>> relations in Task *
>>>
>>>
>>>
>>>
>>> On Sat, Apr 27, 2024 at 4:57 PM Kelvin Macharia  
>>> wrote:
>>>
 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  
> 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
>>  
>> 

Re: Displaying contrast of a queryset

2024-05-01 Thread Kelvin Macharia
Hi  Abdou KARAMBIZI,

Have you tried to make the product field in the Task model optional?

Like:

product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True, 
blank=True)

On Saturday, April 27, 2024 at 9:55:56 PM UTC+3 Abdou KARAMBIZI wrote:

> Hello friends,
>
> products = Task.objects.select_related().all()
> for p in products:  
> print(p.product.product_name)
>
> This gives product that has relation in Task model but *I need product 
> which doesn't have relation in Task *
>
> *Means we have products have relations in Task model and others with no 
> relation in Task model and I need a queryset to display those with no 
> relations in Task *
>
>
>
>
> On Sat, Apr 27, 2024 at 4:57 PM Kelvin Macharia  
> wrote:
>
>> 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  
>>> 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
  
 
 .

>>> -- 
>> 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/4d216a44-5a21-4f3d-aae5-c02553561074n%40googlegroups.com
>>  
>>