I am using a command like the foollwing to get all the distinct values of a column in a a table:
set(deg_course_cat.objects.values_list('degree_code', flat=True))
But the list also returns a '' (empty slots) wherever they are present in the column. How do i give a command to get only the cells with values and not the NULL values?
Filter them out
set((deg_course_cat.objects.filter(degree_code__isnull=False).values_list('degree_code', flat=True))
EDIT
Filter out empty and null results. SO question can be found here
set(
deg_course_cat.objects
.exclude(degree_code__isnull=True)
.exclude(degree_code__exact='')
.values_list('degree_code', flat=True)
)
Related
I have table Users in my database:
id
name
last_name
status
1
John
Black
active
2
Drake
Bell
disabled
3
Pep
Guardiola
active
4
Steve
Salt
active
users_data = []
I would like to get all id and all status row from this db and write to empty dict.
What kind of query should I use? Filter, get or something else?
And what if I would like to get one column, not two?
If, you want to access the values of specific columns for all instances of a table :
id_status_list = Users.objects.values_list('id', 'status')
You can have more info here, in the official documentation
Note that Django provides an ORM to ease queries onto the database (See this page for more info on the queries) :
To fetch all column values of all users instances from your Users table :
users_list = Users.objects.all()
To fetch all column values of specific Users in the table :
active_users_list = Users.objects.filter(status="active")
To fetch all column values of a specific User in the table :
user_33 = Users.objects.get(pk=33)
Use the .values() method:
>>> Users.objects.values('id', 'status')
[{'id': 1, 'status': 'actice'}, {}]
The result is a QuerySet which mostly behaves like a list, you can then do list(Users.objects.values('id', 'status')) to get the list object.
users_data = list(Users.objects.values('id', 'status'))
yourmodelname.objects.values('id','status')
this code show you db in two column include id and status
users_data = list(yourmodelname.objects.values('id','status'))
and with this code you can show your result on dictionary
Suppose your model name is User. For the first part of the question use this code:
User.objects.value('id', 'sataus') # to get a dictionary
User.objects.value_list('id', 'sataus') # to get a list of values
And for the second part of the question: 'And what if I would like to get one column, not two?' you can use these codes:
User.objects.values('id') # to get a dictionary
User.objects.values_list('id') # to get a list of values
User.objects.values('status') # to get a dictionary
User.objects.values_list('status') # to get a list of values
I have inherited a legacy piece of code and do not understand why this code bloc is throwing an Assertion Error, any help is really appreciated!
# rename columns and filter out null values
df_cipSP_PP = cipSP_PP_raw.rename(columns={'Asset/Tag Number': 'cipSP UID'})
df_cipSP_PP_select = df_cipSP_PP.loc[df_cipSP_PP['cipSP UID'].notnull()].copy()
df_cipSP_PP_select['cipSP UID'] = df_cipSP_PP_select['cipSP UID'].astype(str)
# take only one unique uid, eliminating nulls
df_cipSP_PP_rolled = df_cipSP_PP_select.groupby(by='cipSP UID').max().reset_index()
# merge farf against cip sharepoint
PP_sources = pd.merge(how='left', left=PP_farf_cipacc_other2, right=df_cipSP_PP_rolled['cipSP UID'].to_frame(),
left_on='FARF TAG', right_on='cipSP UID')
PP_sources = PP_sources.rename(columns={'cipSP UID': 'CIP'})
This is the error code I am trying to resolve
assert len(locs) == result.shape[1]
df['column_name'].unique()
This will return all the unique values
df['column_name'].unique().tolist() to have all unique values in a list
This would not filter to unique values. This is grouping items by id, and then only taking one of them (as the command says).
To filter unique values, look at the function: unique. It should set you on the right track!
I want to filter mytable & get rows where name does not contain '' or '-' values and for this purpose I have used below query but does not working.
mytable.objects.exclude(name = ['','-']).order_by('name').count() returning 2000 all rows and whereas query mytable.objects.exclude(name = '').order_by('name').count() working perfectly and returning filtered 1460 results.
Below one is my Postgresql query which is working perfectly fine and returning 1450 results excluding name values of ['','-'].
select * from mytable where name != '-' and name != '' order by -id
Try this.
mytable.objects.exclude(name__in=['', '-'])
This should exclude the rows matching the values you've passed in the list.
And you don't need to do order_by() for getting the count.
Just use the .count() directly on queryset.
I have the following model of a blog post:
title = db.Column(db.String())
content = db.Column(db.String())
tags = db.Column(ARRAY(db.String))
Tags field can be an empty list.
Now I want to select all distinct tags from the database entries with max performance - excluding empty arrays.
So, say I have 3 records with the following values of the tags field:
['database', 'server', 'connection']
[]
['connection', 'security']
The result would be ['database', 'server', 'connection', 'security']
The actual order is not important.
The distinct() method should still work fine with array columns.
from sqlalchemy import func
unique_vals = BlogPost.query(func.unnest(BlogPost.tags)).distinct().all()
https://docs.sqlalchemy.org/en/13/orm/query.html?highlight=distinct#sqlalchemy.orm.query.Query.distinct
This would be identical to running a query in postgres:
SELECT DISTINCT unnest(tags) FROM blog_posts
If you can process the results after(usually you can) and don't want to use a nested query for this sort of thing, I usually resort to doing something like;
func.array_agg(func.array_to_string(BlogPost.tags, "||")).label("tag_lists")
and then split on the join string(||) after.
I have the same problem as OP in this thread: Django - filtering by "certain value or None":
I have a field in an data object that can be either null or set to
some integer. I want, given an integer, to filter all the objects with
that integer value OR none:
MyElements.objects.all().filter(value__in=[myInt, None]) # Query 1
However, this line does not work for elements with null value. More
precisely:
MyElements.objects.all().filter(value__in=[None]) # Query 2
returns nothing. whereas
MyElements.objects.all().filter(value = None) # Query 3
returns the null-valued elements.
How can I rewrite the original query (which involves myInt) correctly?
I am aware that OP has accepted an answer, but I would like to know why Query 2 did not yield any results.
It is based on SQL rules for execution. NULL values are not actual values to be compared. it's an unknow value.
read more about this here: is null vs == null
In Django you can get records with NULL values like that:
MyElements.objects.filter(value__isnull = True)
So you need to first filter None out your list of comparisons and add it to your query by yourself.
from django.db.models import Q
MyElements.objects.filter(Q(value__in = [list_of_vals]) | Q(value__isnull = True))
You should test if a field is null using the isnull filter instead; otherwise in SQL any value compared to a null value would always evaluate as false:
from django.db.models import Q
MyElements.objects.filter(Q(value=myInt) | Q(value__isnull=True))