Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between Get and Filter in Django

Shulou Source: shulou.com Published: 2022-06-02 10:10:41 11月18日 Update

This article shows you the difference between Get and Filter in Django. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Define a Model

Class User (models.Model):

Name = models.CharField ('name', max_length=20,default='')

Age = models.CharField ('age', max_length=20,default='')

Simulation data:

User data:

Id name age

1 zhangsan 17

2 lisi 17

First, let's talk about the get usage of Django:

If we want to get a User whose name is zhangsan:

User = User.objects.get (name= "zhangsan")

At this point, you will get a user object whose name is zhangsan

If I want to get a User with an age of 17:

User = User.objects.get (age= "17")

An error is reported at this point, because the Get method: an object result that can only get a unique value. If it matches several objects, it will report an error; if none of them match, it will also report an error.

Second, let's talk about the filter usage of Django:

If we want to get a User whose name is zhangsan:

User = User.objects.filter (name= "zhangsan")

You will get a Queryset object. The first object is the User object of zhangsan, which is the same as the zhangsan obtained by get.

If we want to get a User with an age of 17:

User = User.objects.filter (age= "17")

The test still gets a Queryset object with two objects in it, one is the User object of zhangsan and the other is the User object of lisi

If we get an object that does not exist, we will return an empty [], but this is not a list and cannot be done with the operation method of list

What is the difference between Get and Filter in Django? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Object content skill data method knowledge meeting report concise concise two name namely age article more user article result industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei Shulou Technology Docker Apple