Re: Displaying contrast of a queryset
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(Product, on_delete=models.CASCADE, null=True, blank=True) tasks = Task.objects.filter(product__isnull=True) On Fri, May 3, 2024 at 9:52 AM Abdou KARAMBIZI wrote: > > ThanksKelvin Macharia > > Now it is working properly > > On Wed, May 1, 2024 at 9:24 PM Kelvin Macharia > wrote: > >> 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", "
Re: Displaying contrast of a queryset
ThanksKelvin Macharia Now it is working properly On Wed, May 1, 2024 at 9:24 PM Kelvin Macharia wrote: > 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(mod
Re: Displaying contrast of a queryset
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 >>> <https://groups.google.com/d/msgid/django-users/CABnE44ztqCOVMkfDXHMVDAA0b3DpiyuSDKbQw7SNR9ybUvVLhA%40mail.gmail.com?utm_medium=email_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 > <https://groups.google.com/d/msgid/django-users/4d216a44-5a21-4f3d-aae5-c02553561074n%40googlegroups.com?utm_medium=email_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/CABnE44yO89B2srRGoyzoFgXUbpPSki-mxamXo4GwGYs4zMGugA%40mail.gmail.com.
Displaying contrast of a queryset
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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABnE44ztqCOVMkfDXHMVDAA0b3DpiyuSDKbQw7SNR9ybUvVLhA%40mail.gmail.com.
JOIN
Hello group members, How can I get result containing any information from both 3 models eg. Customer|contact | room number|room price| business| - --- John Doe |+1 989898989|12 | 1000|restaurant class Room(models.Model): room_id = models.AutoField(primary_key=True) room_number = models.CharField(max_length = 200) room_square_meters = models.IntegerField() room_price = models.IntegerField() def __int__(self): return self.room_number class Customer(models.Model): customer_id = models.AutoField(primary_key=True) national_id = models.CharField(max_length=200) first_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) contact_number = models.CharField(max_length=200) customer_status = models.BooleanField(default=True) def __str__(self): return self.first_name+' '+self.last_name class CustomerRooms(models.Model): room = models.ForeignKey(Room,on_delete=models.CASCADE) customer = models.ForeignKey(Customer,on_delete=models.CASCADE) business = models.CharField(max_length = 200) -- 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/CABnE44ym3kYFaKV0xRhHAKRWP9yrG8F%3DX_oPKWPOOLt_FpWtAQ%40mail.gmail.com.
DAILY,MONTHLY AND ANNUAL REPORTS
Hello Friends, How can I get weekly,Monthly and annual reports? from the following model. I want to get the total amount paid in the week,month and year class Payments(models.Model): id = models.AutoField(primary_key = True) amount = models.IntegerField() paidon = models.DateField(auto_now=True) def __str__ (self): return self.id -- 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/CABnE44wPtzkAL5YJ4uh3eRpupXUpH%3DkTJL71obTpTthorCdrTw%40mail.gmail.com.
APP DEPLOYMENT
Hello friends, I want to know where I can upload my django projects for the reason of showing them as my technical experience. -- 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/CABnE44xYg2JqtHVUHMkBkpS81jmQyqfehid3q-%2BnEJe532mrhA%40mail.gmail.com.
Re: Calculations
Thanks Thomas, now it is working properly On Mon, Jul 10, 2023 at 12:17 PM Thomas Couch wrote: > Ah ok, looks like aggregate always returns a dictionary. This should work: > > outstanding_balance = loan.amount_to_pay - > loan.payments_set.aggregate(paid=Sum("amount"))['paid'] > > On Monday, July 10, 2023 at 11:03:06 AM UTC+1 Abdou KARAMBIZI wrote: > >> Hello Thomas >> >> I have tried but I got that error message >> >> On Fri, Jul 7, 2023 at 5:03 PM Thomas Couch wrote: >> >>> Hi Abdou, have a look at aggregation ( >>> https://docs.djangoproject.com/en/4.2/topics/db/aggregation/) >>> >>> In this case I think you'll want something like: >>> >>> ``` >>> from django.db.models import Sum >>> >>> # Where loan = the LoanInformation instance you're interested in >>> outstanding_balance = loan.amount_to_pay - >>> loan.payments_set.aggregate(Sum("amount")) >>> ``` >>> >>> >>> On Thursday, July 6, 2023 at 5:39:50 PM UTC+1 Abdou KARAMBIZI wrote: >>> >>>> class LoanInformation(models.Model): >>>> id = models.AutoField(primary_key=True) >>>> customer = models.ForeignKey(Customer,on_delete=models.CASCADE) >>>> amount_to_pay = models.IntegerField() >>>> >>>> def __str__ (self): >>>> return self.id >>>> >>>> class Payments(models.Model): >>>> id = models.AutoField(primary_key = True) >>>> loan = models.ForeignKey(LoanInformation,on_delete=models.CASCADE) >>>> amount = models.IntegerField() >>>> paidon = models.DateField(auto_now=True) >>>> >>>> def __str__ (self): >>>> return self.id >>>> >>>> >>>> >>>> I want to display the result of "amount_to_pay - sum of amount" >>>> >>> -- >>> 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/1b059aab-1b8e-4a8e-b9e3-d7cb9d1f5673n%40googlegroups.com >>> <https://groups.google.com/d/msgid/django-users/1b059aab-1b8e-4a8e-b9e3-d7cb9d1f5673n%40googlegroups.com?utm_medium=email_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/9be67e3f-785b-4c39-ab6c-309fd33a8289n%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/9be67e3f-785b-4c39-ab6c-309fd33a8289n%40googlegroups.com?utm_medium=email_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/CABnE44yJHw8WB2mPPKj3oJXqdBa854m5qt6ACL3Qcuy3Occt6w%40mail.gmail.com.
Re: Calculations
Hello Thomas I have tried but I got that error message On Fri, Jul 7, 2023 at 5:03 PM Thomas Couch wrote: > Hi Abdou, have a look at aggregation ( > https://docs.djangoproject.com/en/4.2/topics/db/aggregation/) > > In this case I think you'll want something like: > > ``` > from django.db.models import Sum > > # Where loan = the LoanInformation instance you're interested in > outstanding_balance = loan.amount_to_pay - > loan.payments_set.aggregate(Sum("amount")) > ``` > > > On Thursday, July 6, 2023 at 5:39:50 PM UTC+1 Abdou KARAMBIZI wrote: > >> class LoanInformation(models.Model): >> id = models.AutoField(primary_key=True) >> customer = models.ForeignKey(Customer,on_delete=models.CASCADE) >> amount_to_pay = models.IntegerField() >> >> def __str__ (self): >> return self.id >> >> class Payments(models.Model): >> id = models.AutoField(primary_key = True) >> loan = models.ForeignKey(LoanInformation,on_delete=models.CASCADE) >> amount = models.IntegerField() >> paidon = models.DateField(auto_now=True) >> >> def __str__ (self): >> return self.id >> >> >> >> I want to display the result of "amount_to_pay - sum of amount" >> > -- > 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/1b059aab-1b8e-4a8e-b9e3-d7cb9d1f5673n%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/1b059aab-1b8e-4a8e-b9e3-d7cb9d1f5673n%40googlegroups.com?utm_medium=email_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/CABnE44y5XFU0Yy-RqVn76oKkMH-%2BdZczpA0y%3D8OhEGjCKOdOcA%40mail.gmail.com.
JOINING MANY TABLES
Hello all, If I have a house with different floors , a floor has different rooms and rooms have people each room can have one person or may using it and pay rent I want to display in one html table : John is in room1 of floor3 and paid 1000 class Room(models.Model): room_id = models.AutoField(primary_key=True) room_code = models.CharField(max_length = 200) room_square_meters = models.IntegerField() room_price = models.IntegerField() floor = models.ForeignKey(Floor,on_delete=models.CASCADE) is_occupied = models.BooleanField(default=False) def __str__(self): return self.room_code+self.room_price class Customer(models.Model): customer_id = models.AutoField(primary_key=True) first_name = models.CharField(max_length=200) last_name = models.CharField(max_length = 200) contact_number = models.CharField(max_length=200) room = models.ForeignKey(Room,on_delete = models.CASCADE) def __str__(self): return self.first_name + '|' + str(self.contact_number) class Payment(models.Model): payment_id = models.AutoField(primary_key = True) customer = models.ForeignKey(Customer,on_delete=models.CASCADE) starting_date = models.DateField(auto_now=False) ending_date = models.DateField(auto_now=False) amount = models.IntegerField() def __int__(self): return self.amount -- 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/e064be08-5249-4233-959e-1bd8bd4b6a44n%40googlegroups.com.