Django

Advanced QuerySets

Lookups, Q objects, F expressions, aggregation, annotations, select_related, prefetch_related

25 pertanyaan wawancara·
Mid-Level
1

Which lookup allows filtering objects where a field contains a specific string, case-insensitively?

Jawaban

The icontains lookup performs a case-insensitive search to check if a field contains a substring. The 'i' prefix in icontains indicates that the comparison ignores case, which is useful for user searches where case may vary.

2

What is the main difference between select_related and prefetch_related?

Jawaban

select_related uses a SQL JOIN to retrieve related objects in a single query, but only works for ForeignKey and OneToOne relationships. prefetch_related performs separate queries then links objects in Python, making it suitable for ManyToMany and reverse ForeignKey relationships.

3

Which Django object allows combining filter conditions with an OR operator?

Jawaban

Q objects allow building complex queries by combining conditions with | (OR), & (AND) and ~ (NOT) operators. Without Q objects, chained filter() calls only apply AND conditions, making it impossible to create OR queries directly.

4

What is the main advantage of using F() expressions instead of Python values in an update?

5

Which aggregation method returns a dictionary with calculated values instead of a QuerySet?

+22 pertanyaan wawancara

Kuasai Django untuk wawancara berikutnya

Akses semua pertanyaan, flashcards, tes teknis, latihan code review dan simulator wawancara.

Mulai gratis