How Model Queries Work in Django
ฝัง
- เผยแพร่เมื่อ 24 ส.ค. 2024
- Queries are important in any web development framework because you'll be pulling in data for your app to work properly. In this video I show you how to write queries in Django.
Need one-on-one help with your project? I can help through my coaching program. Learn more here: prettyprinted....
Download the code here: s3.us-east-2.a...
Join my free course on working with the database and models in Django: prettyprinted....
Get the Django cheat sheet here: prettyprinted....
Twitter: / pretty_printed
Github: github.com/pre...
Join my free course on working with the database and models in Django: prettyprinted.com/djangodata
Hi, You have a mistake:
Slice[from : to : Step] - this is the correct Slice
Thanks for all of your videos Anthony, great work as always! I've never understood Django's ORM
Thanks!
Where are the joins queries can you also tell that
It's would be nice to mention that exist OR statement in filtering over object for example: result = Item.objects.filter(item.creator = owner) | Item.objects.filter(item.moderated = False)
Also great way in Django is a extracting year or month of date attribute for example: result = Item.objects.filter(date__created__year='2018')
Keep going with great tutorial :)
Yeah, I completely forget about OR queries. I guess I'll have to cover them in a future video. Same thing with dates.
Thank you, much better than reading the documentation.
You're welcome! Thanks for watching.
Blake F. Reading the documentation is effective when you know the basics, the documentation is for advanced things
pretty smooth for a brazillian like me got easy to understand and pretty complete as you said!!! thank you so much sir, ill definitely recommend for coworkers and try the free course you said. Awesome!! keep going calm like this!!
Been looking for solution on how to combine tables in django for 2 days already. And I thank God i found your tutorial
can you help me pls how to combine 3 models?
sir you are the best teacher
This was VERY HELPFUL to me. I am new to Django and was wrestling with the query functionality
I'm glad it helped. Thanks for watching!
Thanks Pretty Printed! Very useful introduction for me. Love the ORM compared to raw SQL :)
The x[5:10] syntax simply means "get items from 5th index to 10th index". I don't know why you complicated it so much.
Very clear and concise explanation , thank you !
DO YOU HAVE VIDEO FOR QUERIES WITHOUT MODELS?
Thank you. I appreciate your effort. 30 minutes back i was unsure how to understand querying a model in easy ways, you just :D ! Respect :)
This video really helped me out a lot. Thanks!
simple, clear and useful to beginners. thanks!
Pretty Printed , briefly. It was awesome and well-articulated. THANk U
Thank you for the great explaination!
Django's pick up lines( easy way to get records) works well on Models. Thank you It was great.
Thanks for watching.
Something I was looking for from long time!! Precious!
How do you return a table value from a another user select table value. For instance returning firstName when selecting LastName from a People table. The value returned from the user selected lastName would populate a select field in a form. As well as the char field to hold the firstName after the user selects the lastName.
Awesome vid. I am just having an issue with using the & query. OR works just fine, but & is returning nothing and I'm stumped.
Hello Sir...!
I have two models A and B which having many to many relation and form third model E with extra attributes .
so i want to perform following sql query using django :
select * from A , B , E where A.id = E.id and B.id = E.id and A.id = '107';
how i can do..?
A.objects().filter(e__aid=107)
geves me only content from A model.
Plzz Help..?
Very clearly explained, thank you
Thank you Sir. You are the best.
Hi pretty printed. How can I create queries and view them on the site(when I do runserver) and not on the terminal's shell?
You need to put the query into your template.
very helpful stuffs . THANKS A LOT !!!
Very usefull stuff. Thank you!
Please add query's using "in" fuction.
dang bro ur awsome thank you for all your content
sir how to create model objects inside the code itself, not using shell or the admin panel
Can you specify types? Like say that that age must be an int instead of a string
Nice overview, very helpful to see the most common Django model queries.
Pretty good info bro .. you have really helped me a lot in my personal project.. your videos are actually really informative n helpful..!!!
I'm glad my videos have helped you. Thanks for watching!
This was super helpful, thank you!
Probably, you forgot :
First object: Model.objects.filter("name").first()
Last object: Model.objects.filter("name").last()
To get random objects: Model.objects.order_by('?')
and Q objects for complex lookups
Thanks for everything :)
Thanks bro such a helpful video!
You're welcome! Thanks for watching.
Why objects.all() not giving all columns ?
Is it possible to join two tables without using foreign key
can you tell me how DateTime field u did?
Hey, How we can query related to joins in django
how to get the id of the particular object for example i need the id of python in the language model can we get it directly if we can what is the syntax for it can u plz help me out
It would be great if you publish .sql file of your bases as well. Or fixture. Offcourse filling them by hand can be good excercise but in case of Programmers table it can be quite daunting task. Other than that - awesome tutorial!
I forgot to add the code link. You can download it here: s3.us-east-2.amazonaws.com/prettyprinted/django_model_queries.zip
We still using Postgres. Can you provide some output file that we can duplicate your DB ?
I have three table a,b,c. A have many to many relation with b and b have many to many relation to c. I have problem when I click on a its show all relative b but I want when I click on a it will show b table entries and all c entries which relative to b
Thank you. Very helpful.
You're welcome! Thanks for watching.
Can someone tell me where to find all these filter keywords (in the documentation)?
Very useful
Thanks
In my project. Objects.all() is throwing error
Please help
Thanks A Lot !
The video was very helpful 😁😊
Do you have any video explaining more about foreign keys ? It seems little confusing ☹️
if its [:5], it simply means get the first five elements of the list, that is; 0,1,2,3,4....exclude 5, same with [5:10].
Thank u so much bro
Your videos have been a great source of information. Actually I need to query a foreign data wrapper (foreign table) of my Postgres db using Django. Is there any way I could do that.
Any leads would be highly appreciated.
Thanks!!
Hi. Would using model queries and using CRUD MySQL methods be different? Thanks
Thanks for the video! Its very clear.
You're welcome!
thanks Great video
Thanks for your work
well explained...thank you
lPlease sir, python.programmers_set this command create many to many relationship between programmers module and ( programme language ) module?
Nice vedio, thanks for making our work more easy, can you say how to user "OR" filter here.
where can I find all filters for queries
Thanks!
Extremely High quality content
Thanks for watching!
Amazing video sir. Very informative
Great video thanks Anthony
Thanks for watching!
It's a very good video
Simple and Good Explanation :)
thanks bro..
Very useful video, Thanks
You're welcome!
Does anyone have any resources on connecting to an MSSQL database? I can connect to the DB and run a stored procedure but struggling with parameters
Thanks Anthony, this is very helpful, I need to know if and how can we save the results of the query into the same model or another model? Could you provide an example, Lets say using boolean field and a list of Integers to save the average of the list, if the boolean is True. Or do you have another video with that kind of example?
If I understand you correctly then all you'd need to do is check if that boolean is true before saving the model. If it doesn't true, then you don't have to do anything.
Great video again, Anthony! Thanks for your videos! Please keep on doing! One doubt: what is the program you use to see your database tables? Is that JADE? Where can I get it? I like the way you check your database. Thanks man! Congratulations!
Yeah, that's JADE. You can get it in the Chrome web store here: chrome.google.com/webstore/detail/javascript-based-database/bponbdjkefbmgkfiiphhabghkkfocook?hl=en
Perfect! Thank you very much! I love it!
Just curious. What software app is being used for your command prompt? The one with the Reddish-Orange circle.
That's Ubuntu on Windows
can we use startwith = '[a-z]' like this ??
Hi Anthony, can you filter from an offset all the way to the end of the table?
i.e:
Programmer.objects.all()[5:]
in order to get the remainder?
Thanks for these videos, they really help me focus down on the basics and remove roadblocks while I get used to Django.
If I understand you correctly, then yeah you can. An offset just skips the first n records. Limits to restrict the number of results, but with no limit, you'll get everything to the end.
Hi Anthony ! Great tutorials ! Thanks a lot ! I have a problem which I can't solve by myself since I'm a beginner. For a example I'm making a website with some articles about dogs and cats. My question is: How to make some kind of button where USER (not admin) can select all the articles about dogs only. Where (which file) should I make a function with sql logic and how should I use it in .html ? Thank you in advance. Paul
You can start by creating two views. One for cats and one for dogs. In each view, you'll query for the category you want and return the results to some html template. Once you get that down, you should see the similarity between the two and you can think of ways you can combine the two views into one.
mypost=post.objects.filter(id__topboard=self).count()
print(mypost)
return mypost
i want to get acount all post for this board get Error like this >>>>
ValueError at /
Cannot query "Schoole": Must be "topic" instance.
can anyone help to solve this
ImportError: cannot import name 'get_user_object' from 'django.contrib.auth'
Thank you... can u show us how we can make class-based URL slug?
I'll add that to my list. Thanks for the idea.
how can i retrieve database tables and all the the data from mysql. if you like, make me a video on mysql, i'm using xampp server. please give me all the details about mysql and how to query data. i have made database using phpmyadmin, now i want to use that data in django app
Hey great and nice video for basics but how you were clearing the shell so quickly was that some trick or shortcut plz share
ctrl + l
Thanks a lot
I'm wondering when we upload data to db should we enter ID or name itself? For example Programmer table column Language. Do you have any videos related to this?
It depends on your situation. For the most part, I like IDs better, but names can work too if they're unique.
Hi there! Thanks for the video! Is there a way to filter by next id? If I want to display the name of my next model on the current one? :)
I've never done something like that before. My approach would be to do it manually. Maybe this well help you: stackoverflow.com/questions/6021694/getting-next-and-previous-objects-in-django
super brooooooo Exlente
works the same, if i use filter.name, instead of filter.name__exact. so why not just use name??
You don't have to. I'm not sure why I did it here. Sometimes I get tunnel vision.
I'm getting an error in the following expression
Message.objects.filter(ishandled == True)
'ishandled' is a field in the Message table and I'm getting error that ishandled is not defined. can you please help?
One equals sign instead of two.
Thank U very much sir!!!!!!!!
You're welcome! Thanks for watching.
how am i supposed to run sql queries in shell like lets say i want to insert then how do i insert something in the table using sql in django shell
Here's more information on that: docs.djangoproject.com/en/2.1/topics/db/sql/#executing-custom-sql-directly
Thanks!
You're welcome! Thanks for watching.
can u help me i querying foreignkey
Great video again... Go ahead :)
Thanks for watching!
any Project with django sir?
name__exact and name__iexact are doing the same thing for me.
>>> Language.objects.filter(name__exact='Java')
>>> Language.objects.filter(name__iexact='Java')
Any video regarding django ORM joins????
Not yet, I'll consider making a video on that.
When I type Company.objects.all().. I'm getting this:
....Why not the names of companies ?
You have to define __str__ in your model class.
Grt ❣️❣️❣️❣️
I love you🖤
I love you too 😘
good
Thanks for the video
i however tried to filter a column from one of my models which has a foreign key but i had to reference it with the primary key instead of the real word. is there a way i can look up for something with out using the primary key. for example Programmer.objects.filter(company__exact='Microsoft') - returned the error but Programmer.objects.filter(company__exact='1') where 1 is the primary key Id for microsoft return no errors how can i make the first statement work. Note: i just used the above example thanks
You would use __ to get into the child model. So company__name__exact='Microsoft'
@@prettyprinted Thanks will try that and revert. do u have any videos on writing templates?
@@kakumbanassirissa3972 Check out my Jinja playlist. It's for Flask, but it's very similar to the default Django template language.
Pretty Printed thanks
Why do you call it 'Jango'? Its with D 'Django'.
1:18 can you tell what that app is and what is used for plz
JADE. It's in the Chrome Chrome app store.
@@prettyprinted thanks can you make video how to use
Here you go: th-cam.com/video/J98ItU1jRq4/w-d-xo.html
20:40 hahaha :D
I almost fell from chair xD
hehehe
I didnt watch the full video because some reason this is hard to follow along without the IDE.
views.py, models.py, ect. dont matter here?
The video covers the basics of the queries, so I use the console instead of the web app so you can see the results instantly. But when you're creating a real app, you'd use the same statements within your views.py file as you do in the console here.