The difference between database query get and filter in Django

1、get:

  • Input parameters: get parameters can only be those fields defined in the model, and only support strict matching.
  • Entry.objects.get(id=‘foo’) # raises Entry.DoesNotExist.
  • Return parameter: The return value of get is a defined model object. It is normal only when one record is returned, which means that the query field of get must be a primary key or a unique constraint field. An exception will be thrown when multiple records are returned or no records are found.

2、filter

  • Input parameter: The parameter of the filter can be a field or an extended where query keyword, such as in, like, and the returned QuerySet contains a new object that matches the given search parameter.
  • Return parameter: filter returns QuerySet object (query result set object. QuerySet refers to the result of query from the database, generally a collection), whether there are matching records or not.
  • The filter has the function of caching data. It queries the database for the first time and generates a cache. If the filter method is called next time, the cached data is directly obtained. The get method directly queries the database every time it is executed.

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/111311582