Re: help been on that for 5 days, Joining 4 tables on 2 columns

2020-07-17 Thread Integr@te System
Hi Omar,

You can look at this to consider:
https://hakibenita.com/django-group-by-sql

Hope this helpful.



On Sat, Jul 18, 2020, 12:32 AM Omar aboul makarem <
omaraboulmaka...@gmail.com> wrote:

>
> Hey Guys please help
>
> I have Approval model, and i want to calculate the sum of total prices of
> the procedures based on the company and procedure type in each Approval
> *https://www.ppaste.org/pwhJ6u7Dg*
> 
> now i want to get all the prices and
> .aggregate(Sum('price'))['price__sum']) to get the sum of all prices of the
> approvals i have.
> the raw sql for postgresql and if my app is called main is :
> now how can i change this sql to django ORM, in the documentation i read
> about select_related(), and F(), but i dunno how to use it in this case
>
> --
> 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/038b6c05-0be2-43b2-8082-f24e08858a92o%40googlegroups.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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAP5HUWpZgzfXpA_JAy4O7CtPRHCGQN7iFb7F9Vwx2qLLCo2Utg%40mail.gmail.com.


help been on that for 5 days, Joining 4 tables on 2 columns

2020-07-17 Thread Omar aboul makarem

Hey Guys please help

I have Approval model, and i want to calculate the sum of total prices of 
the procedures based on the company and procedure type in each Approval
*https://www.ppaste.org/pwhJ6u7Dg* 

now i want to get all the prices and 
.aggregate(Sum('price'))['price__sum']) to get the sum of all prices of the 
approvals i have.
the raw sql for postgresql and if my app is called main is :
now how can i change this sql to django ORM, in the documentation i read 
about select_related(), and F(), but i dunno how to use it in this case

-- 
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/038b6c05-0be2-43b2-8082-f24e08858a92o%40googlegroups.com.


Joining 4 tables on 2 columns

2020-07-16 Thread Omar aboul makarem
class Company(models.Model):
id = models.UUIDField(primary_key = True, default = uuid.uuid4, editable = 
False)
name = models.CharField(_('Name'), max_length=64)


class Approval(models.Model):
 id = models.UUIDField(primary_key = True, default = uuid.uuid4, editable = 
False)
company = models.ForeignKey(Company, related_name=("approval_company"), 
on_delete=models.CASCADE, verbose_name = _('Company'))   
procedure = models.ForeignKey('Procedure', 
related_name=("approval_procedure"), on_delete=models.CASCADE, verbose_name = 
_('Procedure')) 


  
class Procedure(models.Model):
id = models.UUIDField(primary_key = True, default = uuid.uuid4, editable = 
False)
name = models.CharField(_('Name'), max_length=255)



class Price(models.Model):

id = models.UUIDField(primary_key = True, default = uuid.uuid4, editable = 
False)
company = models.ForeignKey('Company', related_name=("price_company"), 
on_delete=models.CASCADE, verbose_name = _('Insurance Company')) 
procedure = models.ForeignKey('Procedure', 
related_name=("price_procedure"), on_delete=models.CASCADE, verbose_name = 
_('Procedure'))   
price = models.IntegerField(_('Price'), )



I have these models, i want to make a queryset that can get me the prices of 
approvals that have been created and aggregate(sum('price'))

I cant find the django way to do it, i know the postgresql raw sql which is 


select main_price.price from main_approval  inner join main_price on 
main_approval.procedure_id = main_price.procedure_id and 
main_approval.company_id = main_price.company_id ;


Keeping in mind that main is my app name


How can i translate that sql in a django way

-- 
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/218172f0-d96e-4cab-b8c5-085b7ccc7d95o%40googlegroups.com.