Prerequisite: Django models
Django lets us interact with its database models, i.e. add, delete, modify and query objects, using a database-abstraction API called ORM(Object Relational Mapper). This article discusses all the useful operations we can perform using Django ORM.
For demonstration purposes, we will use the following Django models.
We can access the Django ORM by running the following command inside our project directory.
python manage.py shell
This brings us to an interactive Python console. Assuming that our models exist in
myProject/albums/models.py we can import our models using the following command:
>>> from books.models import Song, Album
To create an object of model Album and save it into the database, we need to write the following command:
>>> a = Album(title = "Divide", artist = "Ed Sheeran", genre = "Pop") >>> a.save()
To create an object of model Song and save it into the database, we need to write the following command:
>>> s = Song(name = "Castle on the Hill", album = a) >>> s.save()
Let us add 2 more Albums records for the sake of demonstration.
>>> a = Album(title = "Abbey Road", artist = "The Beatles", genre = "Rock") >>> a.save() >>> a = Album(title = "Revolver", artist = "The Beatles", genre = "Rock") >>> a.save()
To retrieve all the objects of a model, we write the following command:
>>> Album.objects.all() <QuerySet [<Album: Divide>, <Album: Abbey Road>, <Album: Revolver>]>
The output is a QuerySet, or a set of objects that match the query. Notice that the name printed is the output of the
We can also filter queries using the functions
filter() function returns a QuerySet having objects that match the given lookup parameters.
>>> Album.objects.filter(artist = "The Beatles") <QuerySet [<Album: Abbey Road>, <Album: Revolver>]>
exclude() function returns a QuerySet having objects other than those matching the given lookup parameters.
>>> Album.objects.exclude(genre = "Rock") <QuerySet [<Album: Divide>]>
get() function returns a single object which matches the given lookup parameter. It gives an error when the query returns multiple objects.
>>> Album.objects.get(pk = 3) <QuerySet [<Album: Revolver>]>
Modifying existing objects
We can modify an existing object as follows:
>>> a = Album.objects.get(pk = 3) >>> a.genre = "Pop" >>> a.save()
To delete a single object, we need to write the following commands:
>>> a = Album.objects.get(pk = 2) >>> a.delete() >>> Album.objects.all() <QuerySet [<Album: Divide>, <Album: Revolver>]>
To delete multiple objects, we can use
exclude() functions as follows:
>>> Album.objects.filter(genre = "Pop").delete() >>> Album.objects.all() <QuerySet >
- Retrieving And Updating Data Contained in Shelve in Python
- Connect Django Project to MongoDB using Django
- Styling Django Forms with django-crispy-forms
- Adding Tags Using Django-Taggit in Django Project
- Python | Updating value list in dictionary
- Merging and Updating Dictionary Operators in Python 3.9
- PyQt5 QCalendarWidget - Updating it
- PyQt5 QCalendarWidget - Updating Micro focus
- Creating and updating PowerPoint Presentations in Python using python - pptx
- PYGLET – Updating Sprite
- PYGLET – Manually Updating Texture of Media in Player
- Python | Inserting item in sorted list maintaining order
- Inserting variables to database table using Python
- PyQt5 QCalendarWidget - Inserting QAction
- PyQt5 QCalendarWidget - Inserting Multiple QAction
- PyQt5 QCommandLinkButton - Inserting actions object
- MoviePy – Inserting Text in the Video
- Python VLC MediaList - Inserting Media
- PYGLET – Inserting Element in Formatted Document
- PYGLET – Inserting Text in Formatted Document
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : NaveenArora