Django: Select count of foreign key field - python

I have the following models:
class SystemTable(models.Model):
id = models.FloatField(primary_key=True)
system_name = models.CharField(max_length=50)
system_desc = models.CharField(max_length=200, blank=True, null=True)
system_url = models.CharField(max_length=2000, blank=True, null=True)
status = models.ForeignKey(StatusLkp, models.DO_NOTHING, db_column='status',default=1)
date_created = models.DateTimeField(default=datetime.now())
objects = models.Manager()
class Meta:
app_label = 'system_table'
managed = True
db_table = 'system_table'
class SystemPagesTable(models.Model):
id = models.FloatField(primary_key=True)
system = models.ForeignKey(SystemTable, on_delete=models.CASCADE)
page_name = models.CharField(max_length=200)
page_link_xpath = models.CharField(max_length=2000)
flag = models.FloatField(blank=True, null=True)
status = models.ForeignKey(StatusLkp, models.DO_NOTHING, db_column='status',default=1)
date_created = models.DateTimeField(default=datetime.now())
objects = models.Manager()
class Meta:
app_label = 'system_pages_table'
managed = True
db_table = 'system_pages_table'
I want to perform the following SQL query:
select
s.*,
(select count(*) from page p where p.system_id = s.id) as NUM_OF_PAGES
from system s;
How do I perform the above query with Django's ORM without having to use a for loop?
I do not want a result based on one system, I want to retrieve all systems with their page counts.

Try to use annotate:
from django.db.models import Count
System.objects.annotate(num_pages=Count('page'))

Related

Django complex filter with two tables

I am in need of some help. I am struggling to figure this out.
I have two models
class Orders(models.Model):
order_id = models.CharField(primary_key=True, max_length=255)
channel = models.CharField(max_length=255, blank=True, null=True)
def __str__(self):
return str(self.order_id)
class Meta:
managed = False
db_table = 'orders'
class OrderPaymentMethods(models.Model):
id = models.CharField(primary_key=True, max_length=255)
payment_type = models.CharField(max_length=75, blank=True, null=True)
fk_order = models.ForeignKey('Orders', models.DO_NOTHING, blank=True, null=True)
class Meta:
managed = False
db_table = 'order_payment_methods'
My goal is to count the number of orders that have a OrderPaymentMethods specific payment_type
Example:
orders = Orders.object.filter(Q(channel="Green_House"))
method_money = orders.filter(payment_methods = "credit").count()
How can I get the count based on the orders that were filtered?
Thank You
You don't need Q nor separating such query. You need to use relation lookup:
Orders.object.filter(channel="Green_House", orderpaymentmethods_set__payment_type="credit").count()

How to save/create thousands of records to postgres using queryset in Django?

I want to store thousands of records in the Student table which has id_users foreign key field. I tried without having foreign key it works smooth but when I added fk constraint it becomes very very slow and took more than 20mins to store 500 records and same without fk took only few seconds.
models - Below models are from different apps
./users/models.py
class Users(models.Model):
id = models.IntegerField(primary_key=True)
cts = models.DateTimeField(blank=True, null=True)
uts = models.DateTimeField(blank=True, null=True)
id_states = models.ForeignKey(States, models.DO_NOTHING, db_column='id_states', blank=True, null=True)
id_user_type = models.ForeignKey(UserType, models.DO_NOTHING, db_column='id_user_type')
firstname = models.TextField(blank=True, null=True)
lastname = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'users'
./student/models.py
class Student(models.Model):
id = models.BigAutoField(primary_key=True)
cts = models.DateTimeField()
uts = models.DateTimeField()
id_users = models.ForeignKey('users.Users', models.DO_NOTHING, db_column='id_users')
num_subjects = models.IntegerField()
num_teachers = models.IntegerField()
class Meta:
managed = False
db_table = 'students'
bulk_create() method -
def saveCounts(cumulative_counts: pd.DataFrame, model: django.db.models.base.ModelBase) -> None:
records = cumulative_counts.to_dict('records')
model_instances = [model(
cts=timezone.now(),
uts=timezone.now(),
id_users=Users.objects.get(
id=record['id_users']),
num_subjects=record['num_subjects'],
num_teachers=record['num_teachers'],
) for record in records]
model.objects.bulk_create(model_instances)

How to get result with ORM from queryset. (python, django)

I want to get result with ORM of Django. But I cannot get result with ORM.
I just tried with 'select_related("field_name")'. but doesn't work..
Actually, I don't know using ORM well haha...
and..
This Query is my needs
SELECT sol.idx, sol.sol_name, sol.sol_warning, sol.user_num_id, sol.auto_run_server, ur.user_ip,
pc.cpu_usage, pc.mem_usage, pc.disk_usage FROM solution AS sol
INNER JOIN ems.user AS ur ON sol.user_num_id = ur.user_num INNER JOIN pc_status AS pc ON sol.user_num_id = pc.user_num_id;
result row is ...
idx, sol_name, sol_warning, user_num_id, auto_run_server, user_ip, cpu_usage, mem_usage, disk_usage
so..
I used code is " Solution.objects.select_related('user_num') "
But I cannot get same result.
here are my python codes.
models.py
class User(models.Model):
user_num = models.IntegerField(primary_key=True, auto_created=True)
user_ip = models.CharField(max_length=20)
user_name = models.CharField(max_length=10)
user_pwd = models.CharField(max_length=10)
class Meta:
db_table = 'user'
class Solution(models.Model):
idx = models.IntegerField(primary_key=True, auto_created=True)
sol_name = models.CharField(max_length=50)
sol_warning = models.IntegerField()
user_num = models.ForeignKey(User, on_delete=models.CASCADE)
auto_run_server = models.IntegerField()
class Meta:
db_table = 'solution'
class PcStatus(models.Model):
pc_idx = models.IntegerField(primary_key=True, auto_created=True)
cpu_usage = models.IntegerField(default=0)
mem_usage = models.IntegerField(default=0)
disk_usage = models.IntegerField(default=0)
user_num = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
db_table = 'pc_status'

DRF - filter across 2 models

I'm working with a legacy database where I have a serializer setup on Table A like so -
class TblapplicationsSerializer(serializers.ModelSerializer):
class Meta:
model = Tblapplications
fields = ('applicationid', 'applicationname', 'description', 'drtierid', 'saglink', 'supportinstructions',
'defaultincidentpriorityid', 'applicationorigintypeid', 'installationtypeid', 'comments',
'lastmodifieddate', 'lastmodifiedby', 'assetstatusid', 'recordownerid', 'adl_app')
depth = 2
I'm using a standard filter -
class TblapplicationsFilter(django_filters.FilterSet):
name = django_filters.CharFilter(name="applicationname", lookup_type="exact")
env = django_filters.CharFilter(name="adl_app__environmentid__domain")
class Meta:
model = Tblapplications
fields = ['applicationname', 'name', 'env']
Here's where it goes sideways. What I want to be able to do is filter on my URL like /api/applications/?name=xxx&env=DEV. It would then return the application and any databases that are linked with the environment of DEV. The name was understandably easy, but the only was I figured out the environment was to make the api point for applications touch the middle table that links the two but it returns multiple values because it's grabbing each time application is referenced with a separate database.
I've updated the Serializer and Filter based on comments given and the serializer, without the &env=DEV returns all the appropriate data (domain is nested in a reverse relationship). I then want my filter to filter the results based on that. Which means that it needs to somehow know to limit the results on the reverse relationship to only what's provided from the nested value.
If you see my models -
class Tblapplicationdatabaselinks(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
applicationid = models.ForeignKey('Tblapplications', db_column='applicationId', to_field='applicationid',
related_name='adl_app')
dbid = models.ForeignKey('Tbldatabases', db_column='dbId', to_field='id', related_name='adl_db')
environmentid = models.ForeignKey('Tbldomaincodes', db_column='environmentId', to_field='id',
related_name='adl_envlink')
comments = models.TextField(blank=True)
lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
# upsize_ts = models.TextField(blank=True) # This field type is a guess.
class Meta:
managed = False
db_table = 'tblApplicationDatabaseLinks'
class Tblapplications(models.Model):
applicationid = models.AutoField(db_column='applicationId', primary_key=True)
applicationname = models.CharField(db_column='applicationName', max_length=255)
description = models.TextField(blank=True)
drtierid = models.ForeignKey(Tbldomaincodes, db_column='drTierID', blank=True, null=True, to_field='id',
related_name='app_drtier')
saglink = models.TextField(db_column='sagLink', blank=True)
supportinstructions = models.TextField(db_column='supportInstructions', blank=True)
defaultincidentpriorityid = models.IntegerField(db_column='defaultIncidentPriorityId', blank=True, null=True)
applicationorigintypeid = models.IntegerField(db_column='applicationOriginTypeId')
installationtypeid = models.ForeignKey(Tbldomaincodes, db_column='installationTypeId', to_field='id',
related_name='app_insttype')
comments = models.TextField(blank=True)
assetstatusid = models.ForeignKey(Tbldomaincodes, db_column='assetStatusId', to_field='id',
related_name='app_status')
recordownerid = models.ForeignKey(Tblusergroups, db_column='recordOwnerId', blank=True, null=True,
to_field='groupid', related_name='app_owner')
lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
# upsize_ts = models.TextField(blank=True) # This field type is a guess.
class Meta:
managed = False
db_table = 'tblApplications'
class Tbldatabases(models.Model):
dbid = models.AutoField(db_column='dbId', primary_key=True)
dbname = models.CharField(db_column='dbName', max_length=255)
serverid = models.ForeignKey('Tblservers', db_column='serverId', to_field='serverid', related_name='db_serv')
servicename = models.CharField(db_column='serviceName', max_length=255, blank=True)
dbtypeid = models.IntegerField(db_column='dbTypeId', blank=True, null=True)
inceptiondate = models.DateTimeField(db_column='inceptionDate', blank=True, null=True)
comments = models.TextField(blank=True)
assetstatusid = models.IntegerField(db_column='assetStatusId')
recordownerid = models.IntegerField(db_column='recordOwnerId', blank=True, null=True)
lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
# upsize_ts = models.TextField(blank=True) # This field type is a guess.
class Meta:
managed = False
db_table = 'tblDatabases'
class Tbldomaincodes(models.Model):
id = models.IntegerField(db_column='ID', primary_key=True)
domain = models.CharField(primary_key=True, max_length=255)
displayname = models.CharField(db_column='displayName', primary_key=True, max_length=255)
displayorder = models.IntegerField(db_column='displayOrder', blank=True, null=True)
comments = models.TextField(blank=True)
lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True)
lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True)
# upsize_ts = models.TextField(blank=True) # This field type is a guess.
class Meta:
managed = False
db_table = 'tblDomainCodes'
Extend your filter set and reference the field in the other model:
class TblapplicationsFilter(django_filters.FilterSet):
name = django_filters.CharFilter(name="applicationname", lookup_type="exact")
env = django_filters.CharFilter(name="environmentid__name")
# ^^^^^^^^^^^^^^^^^^^
class Meta:
model = Tblapplications
fields = ['applicationname', 'name', 'env']
Also, you may wish to name your ForeignKey fields without the id suffix, which is the Django convention. In Django, when you access Tblapplications.environmentid, it is normally a model instance, not the actual id integer itself.

Pulling Data From Multiple Tables in Django

I'm kind of new to Django and am having some trouble pulling from existing tables. I'm trying to pull data from columns on multiple joined tables. I did find a solution, but it feels a bit like cheating and am wondering if my method below is considered proper or not.
class Sig(models.Model):
sig_id = models.IntegerField(primary_key=True)
parent = models.ForeignKey('self')
state = models.CharField(max_length=2, db_column='state')
release_id = models.SmallIntegerField(choices=releaseChoices)
name = models.CharField(max_length=255)
address = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=255, blank=True)
zip = models.CharField(max_length=10, blank=True)
phone1 = models.CharField(max_length=255, blank=True)
fax = models.CharField(max_length=255, blank=True)
email = models.EmailField(max_length=255, blank=True)
url = models.URLField(max_length=255, blank=True)
description = models.TextField(blank=True)
contactname = models.CharField(max_length=255, blank=True)
phone2 = models.CharField(max_length=255, blank=True)
ratinggroup = models.BooleanField()
state_id = models.ForeignKey(State, db_column='state_id')
usesigrating = models.BooleanField()
major = models.BooleanField()
class Meta:
db_table = u'sig'
class SigCategory(models.Model):
sig_category_id = models.IntegerField(primary_key=True)
sig = models.ForeignKey(Sig, related_name='sigcategory')
category = models.ForeignKey(Category)
class Meta:
db_table = u'sig_category'
class Category(models.Model):
category_id = models.SmallIntegerField(primary_key=True)
name = models.CharField(max_length=255)
release_id = models.SmallIntegerField()
class Meta:
db_table = u'category'
Then, this was my solution, which works, but doesn't quite feel right:
sigs = Sig.objects.only('sig_id', 'name').extra(
select = {
'category': 'category.name',
},
).filter(
sigcategory__category__category_id = categoryId,
state_id = stateId
).order_by('sigcategory__category__name', 'name')
Now since the items in filter() join the sigcategory and category models, I was able to pull category.name out by using extra(). Is this a proper way of doing this? What if I did not have the reference in filter() and the join did not take place?
SigCategory has a ForeignKey pointing at Category, so you can always get from the SigCategory to the Category simply by doing mysigcategory.category (where mysigcategory is your instance of SigCategory.
If you haven't previously accessed that relationship from that instance, doing it here will cause an extra database lookup - if you're concerned about db efficiency, look into select_related.

Categories