How To Catch A Unicorn: Django ORM Methods

How To Catch A Unicorn: Django ORM Methods 

This post isn't really about how to catch a unicorn (sorry) - but it is the name of a very very beautiful and magical play and piece of puppet theatre that I saw at my primary school, put on by the guy who was also the swimming teacher. Because ORM methods have been a bit mystical to me for some time, this title seems appropriate.

Really getting it at last does finally feel like catching a unicorn.

An image of woodland drawings and pastel methods. Text reads: Django methods I learned properly and it lists all the methods discussed below. Pictures of unicorns too.


I am back at work! I forgot how much I loved it, forgot how happy coding makes me. I forgot how much my love for coding gives my life meaning and purpose. It's all coming back to me now. It's so good to be back. I have missed the lovely people in my local Brighton office.

And now it's time to get back to the ORM methods!

Revising/Learning ORM methods in Django

I am doing this tutorial. I have started recommending it to people who want to learn Django, amongst others.

The Methods I Have Learned So Far

  • objects.all()
  • objects.create()
  • objects.get()
  • objects.filter()
  • objects.order_by()

An Example

Here as an example of some things I might write in the Django version of the Python shell. It might not be comprehensive so please watch out. It's just an example of some of the methods I might write.

So to get the Python shell running (but the one that has Django magic under the surface as I am working in a Django project) I might write this:

python manage.py shell

And then let's imagine that I'm working on a project called Recipes. So I have created a Recipe model in my models.py folder. And I have created a model in the backend with all of the data that I need to create a Recipe object. Because, EVERY RECIPE OBJECT IS AN INSTANCE OF THE RECIPE CLASS, EVERY RECIPE IS A ROW ON THE TABLE. OMG I GET IT OMG. 

MODELS ARE JUST TABLES IN DJANGO. 

OF COURSE I KNEW THAT BEFORE, BUT IT'S ANOTHER THING TO GET IT OMG. 

EVERY INSTANCE OF AN OBJECT IS JUST ANOTHER ROW OF THE TABLE. IS JUST ANOTHER OF INSTANCE OF THE CLASS WE HAVE DEFINED. EVERY CLASS IS LIKE A TABLE IN A MODEL SORT OF - EVERY ROW ON THAT TABLE IS AN INSTANCE OF THAT CLASS. NOOOOOOOOOO I GET IT! I GET IT NOOOOOOO! I CAN'T BELIEVE I FINALLY GET IT NOW! OMG! THANK YOU.

Okay so we have created our model and migrated it and made the migrations.

What methods can we use?

Methods

from coobook.models import Recipe

(btw I am making this all up btw thanks). 

If you want to see which Recipes you already have then you can run:

Recipe.objects.all()

If I want to create a new object

Then all I have to do is run this:

Recipe.objects.create()

with the attributes as the parameters:

Recipe.objects.create(name="Fruit Salad", ingredients="Mango, Pineapple, Kiwi", instructions= "1. Chop fruits, 2. Put in bowl, 3. Mix them")

So then you have a new object. (I believe; this syntax isn't perfect; I'm typing it straight into here. But I believe it should be fine.). Thank you. So what's next then?

How To Catch A Unicorn

Okay so this is where it gets a bit harder. But this is also really the relevant part of the tutorial. So I have to really focus on that here.

Thanks.

objects.get() method

objects.get() is designed to retrieve a SINGLE, SPECIFIC item from the database.

So let's say I just want to get the object with the name of Fruit Salad. All I would need to do is go:

Recipe.objects.get(name="Fruit Salad")

Can get() have multiple parameters? Yes, it can indeed.

objects.filter() method

objects.filter() is designed to retrieve multiple items from the database that match the lookup parameters.

So the filter method returns all of the instances of an object in the Django model where a certain condition is met. In that sense it is like a WHERE clause in an SQL query.

So imagine we want to get from our Recipes Model all of the recipes that contain Mango.

The query might look something like this:

recipes_with_mango = Recipe.objects.filter(ingredients__icontains="Mango").

icontains apparently means that the query is case-insensitive.

This will return all of the recipes in our model that contain mango as an ingredient.

I am afraid I got too tired today

It's 6 pm and I am already pleased with myself for coming on to wrap up.

I will finish this tomorrow. I still have order_by() and save() to do.

Working In The Python Shell

Embarrassingly, I haven't done much of this before. 

You just type in python into a terminal. Um, whoops.

It might have to be imported or installed or something - I don't know - but I guess I have it all installed anyway.

It's been really fun playing around with it and creating some new functions and testing them out. It is a really useful and valuable skill to have this in Python so yeah. Really amazing skill to know and really happy and proud of myself that I played around and learned it today - of course I have seen my seniors do it a million times but nice to try it out for myself. Remember I am still quite new to Python.



Comments

Popular posts from this blog

Hello World

“But yesterday, I heard God say, you were born to be the one…”

Yosemite