Cheat sheet .... Welcome ! #***(1)Returns all customers from customer table customers = Customer.objects.all() #(2)Returns first customer in table firstCustomer = Customer.objects.first() #(3)Returns last customer in table lastCustomer = Customer.objects.last() #(4)Returns single customer by name customerByName = Customer.objects.get(name='Peter Piper') #***(5)Returns single customer by name customerById = Customer.objects.get(id=4) #***(6)Returns all orders related to customer (firstCustomer variable set above) firstCustomer.order_set.all() #(7)***Returns orders customer name: (Query parent model values) order = Order.objects.first() parentName = order.customer.name #(8)***Returns products from products table with value of "Out Door" in category attribute products = Product.objects.filter(category="Out Door") #(9)***Order/Sort Objects by id leastToGreatest = Product.objects.all().order_by('id') greatestToLeast = Product.objects.all().order_by('-id') #(10) Returns all products with tag of "Sports": (Query Many to Many Fields) productsFiltered = Product.objects.filter(tags__name="Sports") ''' (11)Bonus Q: If the customer has more than 1 ball, how would you reflect it in the database? A: Because there are many different products and this value changes constantly you would most likly not want to store the value in the database but rather just make this a function we can run each time we load the customers profile ''' #Returns the total count for number of time a "Ball" was ordered by the first customer ballOrders = firstCustomer.order_set.filter(product__name="Ball").count() #Returns total count for each product orderd allOrders = {} for order in firstCustomer.order_set.all(): if order.product.name in allOrders: allOrders[order.product.name] += 1 else: allOrders[order.product.name] = 1 #Returns: allOrders: {'Ball': 2, 'BBQ Grill': 1} #RELATED SET EXAMPLE class ParentModel(models.Model): name = models.CharField(max_length=200, null=True) class ChildModel(models.Model): parent = models.ForeignKey(Customer) name = models.CharField(max_length=200, null=True) parent = ParentModel.objects.first() #Returns all child models related to parent parent.childmodel_set.all()
I've done 2 Udemy Django classes and 2 youtube tutorial series and this is by far the best of them. The way you take your time to make the slides and layout the queries function and example shows that you care about us. thanks.
Dennis I cannot stress how helpful and clear your work is. Other channels don’t explain properly or complicate it too much when the point is to introduce simple topics. Im sure you are going a long way ;) Wish you all the best mate!
Dennis thanks a lot for this tutorial. My question is the queriesDemos.py file, do we have to create one for whatever app we are building before we can actually query our database in our templates?
i'm getting an error working on the shell after print(customers) return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table:
Hello Sir can you help me in this...? 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..?
Hi Dennis. Your video is awesome. It is really nice and understandable. Keep going. And I think you are the best Django teacher because on Russian youtube or American I don't see normal videos. You the best #DennisIvyTheBest #2020
Thank you Dennis. This Django series is by far the best series on the internet. Learning a lot from it. Learnt Python from CodeWithHarry, and learning Django from you. I can't thank you enough for this awesome tutorial. Love from Nepal Brother.
WARNINGS: accounts.Customer: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. accounts.Order: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. accounts.Product: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. accounts.Tag: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the AccountsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. I'm getting this error can you help please
Hi Dennis thank you for your youtube tutorials I know it has been a long time since you posted this one, but I got to ask the source code is not working is there any way we can get them again thank you keep up the great work
Hola comunidad una pregunta como puedo pasar esta consulta sql a mi template desde la vista en forma de ciclo query = 'SELECT mantenimiento_mantenimiento.id ,mantenimiento_mantenimiento.phone , mantenimiento_mantenimiento.user , usuarios_user.first_name FROM mantenimiento_mantenimiento, usuarios_user where mantenimiento_mantenimiento.user = usuarios_user.id' #for list_art in Mantenimiento.objects.raw(query):
Hi dennis with regards to the query about "number of balls a customer has ordered" why have you used the double underscores whereas the relationship between a customer and an order is one to many relationship? this is the query i am referring to ballOrders = firstCustomer.order_set.filter(product__name="Ball").count()
Great question :) Double underscores can be used in many ways. In this case I use it to chain upwards and access an attribute of the product model (name). Because our query starts at customer we had to go three levels deep: Customer --> Order --> Product 1. We query all orders to that customers with "order_set.filter()" 2. Then we need to get all orders that are related to the product with the attribute of "ball" so we access the product.name by doing product__name in the "filter()" method. Sorry I hope that makes sense. Kind of tough to explain.
I'd have to do some research on that myself. Haven't really customized any fields outside of adding some widgets. I'm make a video on this once i do some more research :)
I'm not gonna lie. This channel is awesome. Got udemy courses and some youtube playlist. But this MAN really THE BEST OF IT. Thanks sir. SAVE ME ON MY SCHOOL PROJECT
Hello, I'm a little bit late ... but ... why does the query : product = Product.objects.filter(tags__name="Sports") --> works, but the query: item = Tag.objects.filter(tags__name="Ball") does not? Product and Tag are in a many to many relationship, so i thought, it wold work in both directions. >>> from accounts.models import * >>> item = Tag.objects.all() >>> print(item) >>> product = Product.objects.all() >>> print(product) >>> product = Product.objects.filter(tags__name="Sports") >>> print(product) >>> item = Tag.objects.filter(tags__name="Ball") Traceback (most recent call last): File "", line 1, in ....snipp.... django.core.exceptions.FieldError: Cannot resolve keyword 'tags' into field. Choices are: id, name, product >>> Thank you ...
Hi Dennis Ivy .I am following your tutorials i really like the way you are explaining the things . can you please suggest other projects so i can work as a beginner to explore more about django. Thanks
Hi guys, hi Dennis. Thank you very much for your effort in creating this course. I have a question related to the ball case in the last part of the video, wouldn't it be easier to add a quantity field to the order table for each chosen item?
A great explanation and a superb set of tutorials. One question. Could not an order have many products as opposed to just a single one in your example?
In this case no. Its a purchased item and not a set of orders. So lets say you place an order on amazon, you can have a shopping cart full of orders that relate to many products but each individual order must tie only to one product. You could however have quantity attribute. Is that what you were thinking?
Thanks for your precious service to humanity. I nearly quit Django and I chanced on your videos on youtube. You have inspired new hope in me, and have increased my passion for Django. I am amazed by your selfless nature, and your willingness to share knowledge and help others.
So far so good. This tutorial is really detailed, organized and understandable. If you make all of your tutorials like this eventually people gonna discover this channel. You can be the next "code with mosh".
Thanks so much Denis. Can't believe that your tutorials are free. Better than Python Django Bootcamp that I bought on Udemy which is never updated. Usually I don't subscribe to RU-vid but has to do it on yours because of how good it is. Keep it up.