What is the difference between
mymodel=model.objects.get(name='pol')
and
mymodel=model.objects.filter(name='pol')
What is the difference between
mymodel=model.objects.get(name='pol')
and
mymodel=model.objects.filter(name='pol')
get() returns an object that matches lookup criterion.
filter() returns a QuerySet that matche lookup criterion.
For example, the following
Entry.objects.filter(pub_date__year=2006)
is equivalent to
Entry.objects.all().filter(pub_date__year=2006)
which means filter() is slightly expensive operation if the model class has a large number of objects, whereas get() is direct approach.
source: Django making queries
Another difference:
Because 'get' returns an object, the method 'update' cannot be called on the object; except a model method (which shouldn't be done, to avoid overriding), was written.
However, querying with 'filter', gives you the ability to update a preferred record.
e.g: say a model; 'Product' exists in your app; then you could do thus:
old_product_name = Product.objects.filter(product_name="green-peas")
old_product_name.update(product_name="skewed-peas")
That of course, isn't possible when you query with 'get'.
Django's get and filter methods are commonly used by django models, and are distinguished here.
Define 2 models.
class Student(models.Model):
Name = models.CharField('name', max_length=15, default='')
Age = models.CharField('age', max_length=15, default='')
class Book(models.Model):
student = models.ForeignKey(Student)
A.django get method:
django's get method is to get a matching result from the database, return an object, if the record does not exist, it will report an error. For example, if there is a record in my database, the value of the record name is django123, I use student = Student . objects . get ( name = ’ django123 ’ ), returns a record object, which you can view through student . _ _ dict _ _, which returns a dictionary form, {' key ' : valeus}, key is the field The name, while values are the contents of the value. Using the get method to query a record that does not exist in the database, the program will report an error. For example: student = Student . objects . get ( name = ’ django ’ ), obviously this 'django ' does not exist in the database, it will report an error.
If you use django's get to get the data of the associated table, if the data of the key table is more than 2, it will report an error. If there is a record in the student table:
d name age 1 python 24 Book table: id student_id 1 1 2 1
student = Student.objects.get(name='python')
book = Book.objects.get(student)
The result will be reported because the book table has 2 records that match the student table.
Two.django filter method:
django's filter method is to get a matching result from the database, return a list of objects, if the record does not exist, it will return [ ]. If the database has a record, the value of the record name is python123, using student = Student . objects .filter ( name = ’ python123 ’ ). Returning student is a list of objects. It can be seen that student [ 0 ] is the same as the student returned by the get method above.
If you use django's get to get the data of the associated table, no matter how many records in the associated table, no error will be reported. django In addition to the powerful model, forms and templates are also very powerful. filter has the function of caching data. The first time you query the database and generate the cache, the next time you call the filter method, you can directly get the cached data, and the get method is directly queried every time in database.
Employee : name,age,loc Get:
Employee.objects.get(id=1) It will through error if given object is not there Filter : Employee.objects.filter(id=1) ]> It will return empty Queryset if given object is not there