Playing with Django Model Objects – CheatSheet

One must know how to play with Django models in their views in order to create efficient and short functions.

Let’s take a model for example.

class Teacher(models.Model):
name = models.CharField(max_length=100)

class Student(models.Model):


This content originally appeared on DEV Community and was authored by Priyanshu Panwar

One must know how to play with Django models in their views in order to create efficient and short functions.

Let's take a model for example.

class Teacher(models.Model):
    name = models.CharField(max_length=100)

class Student(models.Model):
    name = models.CharField(max_length=100)
    roll = models.CharField(max_length=100)
    mentor = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    reg_date = models.DateTimeField(auto_add_now=True)

Extracting all objects of a model

Let's extract all the students.

students = Student.objects.all()

Extracting a student by ID

ID is the primary key in every model.

from django.shortcuts import get_object_or_404

def my_view(request):
    obj = get_object_or_404(MyModel, pk=1)

Or there is another way to do this.

stud = Student.objects.get(pk=1)

The last one returns a error in case a student doesn't exist with the following id.

Filtering the objects

Simple filtering can be done with equating like

studs = Student.objects.filter(name='Ram Kapoor')

This will return the list of students whose name is Ram Kapoor.
We can also refer to the details of an attribute with the symbol __.

stud_2006 = Student.objects.filter(reg_date__year=2006)

This will return all the students registered in 2006.

stud_p = Student.objects.filter(name__startswith='P')

This will return all the students whose names start with 'P'.

Using Q() - Very Powerful

This is used to add many filters in a single filter using | (or), & (and).

stud = Student.objects.filter(Q(name__startswith='P') | Q(reg_date__year=2006))

This will return both the students whose names start with 'P' or who are registered in year 2006.

THANK YOU

Find me on Priyanshu Panwar | LinkedIn


This content originally appeared on DEV Community and was authored by Priyanshu Panwar


Print Share Comment Cite Upload Translate Updates
APA

Priyanshu Panwar | Sciencx (2021-11-07T17:18:40+00:00) Playing with Django Model Objects – CheatSheet. Retrieved from https://www.scien.cx/2021/11/07/playing-with-django-model-objects-cheatsheet/

MLA
" » Playing with Django Model Objects – CheatSheet." Priyanshu Panwar | Sciencx - Sunday November 7, 2021, https://www.scien.cx/2021/11/07/playing-with-django-model-objects-cheatsheet/
HARVARD
Priyanshu Panwar | Sciencx Sunday November 7, 2021 » Playing with Django Model Objects – CheatSheet., viewed ,<https://www.scien.cx/2021/11/07/playing-with-django-model-objects-cheatsheet/>
VANCOUVER
Priyanshu Panwar | Sciencx - » Playing with Django Model Objects – CheatSheet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/07/playing-with-django-model-objects-cheatsheet/
CHICAGO
" » Playing with Django Model Objects – CheatSheet." Priyanshu Panwar | Sciencx - Accessed . https://www.scien.cx/2021/11/07/playing-with-django-model-objects-cheatsheet/
IEEE
" » Playing with Django Model Objects – CheatSheet." Priyanshu Panwar | Sciencx [Online]. Available: https://www.scien.cx/2021/11/07/playing-with-django-model-objects-cheatsheet/. [Accessed: ]
rf:citation
» Playing with Django Model Objects – CheatSheet | Priyanshu Panwar | Sciencx | https://www.scien.cx/2021/11/07/playing-with-django-model-objects-cheatsheet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.