Displaying contrast of a queryset

165 views
Skip to first unread message

Abdou KARAMBIZI

unread,
Apr 26, 2024, 5:41:10 AMApr 26
to django...@googlegroups.com
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


Muhammad Juwaini Abdul Rahman

unread,
Apr 26, 2024, 10:22:26 AMApr 26
to django...@googlegroups.com
You're looking for `product`, but the model that you queried is `Task`.

--
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.

Kelvin Macharia

unread,
Apr 27, 2024, 10:57:25 AMApr 27
to Django users
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

Abdou KARAMBIZI

unread,
Apr 27, 2024, 2:55:56 PMApr 27
to django...@googlegroups.com
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 




manohar chundru

unread,
Apr 28, 2024, 1:06:27 PMApr 28
to django...@googlegroups.com

Kelvin Macharia

unread,
May 1, 2024, 2:10:03 AMMay 1
to Django users
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)

Ryan Nowakowski

unread,
May 1, 2024, 9:59:46 AMMay 1
to django...@googlegroups.com
Products.objects.filter(task_set__isnull=True)

Kelvin Macharia

unread,
May 1, 2024, 3:23:14 PMMay 1
to Django users
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)

Abdou KARAMBIZI

unread,
May 3, 2024, 3:53:59 AMMay 3
to django...@googlegroups.com

Thanks

Kelvin Macharia 


Now it is working properly 

Abdou KARAMBIZI

unread,
May 19, 2024, 1:54:39 PMMay 19
to django...@googlegroups.com
  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

 I want all products that a user logged in didn't send in task model
The following query is working but  when a user logs in should get all products he/she didn't send in task model

product = models.ForeignKey(Producton_delete=models.CASCADEnull=Trueblank=True)
tasks = Task.objects.filter(product__isnull=True)

Natraj Kavander

unread,
May 20, 2024, 10:34:06 PMMay 20
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages