ZTP Interview Flashcards

(7 cards)

1
Q

What is hybrid inheritance in Django

A

A class can inherit properties from multiple base classes.

A class can inherit from more than one base class while also serving as a base class for other classes, creating complex hierarchical inheritance. Chain of inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does Django handle url routing?

A

Django handles URL routing using urls.py files where URL patterns are mapped to view functions using path() or re_path(). The main urls.py can include app-specific URLs via include(). Dynamic parts of the URL (e.g., <int:id>) can be captured and passed to views. Named routes (name=...) allow easy reference in templates and redirects and tests.</int:id>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the difference between path and re_path in Django and when would you use each?

A

path() - std, preferred as its more maintainable and readable

re_path() - when regex needed

In Django, path() and re_path() are both used for routing URLs to views, but they serve slightly different purposes:

path():

•	Use it when your URL pattern is simple and can be expressed with straightforward path converters like:
•	int, str, slug, uuid, path
•	It’s cleaner and easier to read.
•	Example:

path(‘articles/<int:year>/', views.year_archive)</int:year>

re_path():

•	Use it when you need regular expression (regex) matching for complex URL patterns that path() can’t express.
•	It offers more control and flexibility (but is harder to read and maintain).
•	Example:

re_path(r’^articles/(?P<year>[0-9]{4})/$', views.year_archive)</year>

Interview Tip:

Favor path() when possible, as it’s the modern and more readable approach introduced in Django 2.0. Use re_path() only when you need regex-level control.

Would you like to practice answering this yourself, or move to another question in the same area?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the differences between select_related and prefetch_related in Django, and when would you use each?

A

In Django, both select_related and prefetch_related are used to optimize database queries and reduce the number of queries made when accessing related objects. However, they work differently and are used in different situations.

select_related:

•	Used for single-valued relationships like ForeignKey and OneToOneField.
•	It performs a SQL join and includes the related object in the same query.
•	This is more efficient when you need to access related objects for many items.
•	Example:

articles = Article.objects.select_related(‘author’)

This fetches Article and related Author objects in one SQL query.

prefetch_related:

•	Used for many-valued relationships, like ManyToManyField and reverse ForeignKey relationships.
•	It performs two separate queries and joins the data in Python.
•	It’s useful when you can’t use joins (e.g., many-to-many), or when you want to fetch and cache related objects efficiently.
•	Example:

authors = Author.objects.prefetch_related(‘articles’)

This runs two queries: one for authors and one for their related articles.

Summary:

•	Use select_related for one-to-one or foreign key relationships.
•	Use prefetch_related for many-to-many or reverse relationships.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a reverse foreign key relationship?

A

A reverse foreign key relationship occurs when you access the related objects from the model that doesn’t define the ForeignKey, but is referenced by it.

Example:

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

class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)

Here:

•	Book has a ForeignKey to Author (i.e. a book has one author).
•	From the Book side, you access the related Author using the book.author attribute — this is the forward relationship.

But from the Author side, you can access all related Book instances using:

author.book_set.all()

This is the reverse foreign key relationship — accessing the “many” side (books) from the “one” side (author).

You can customize the reverse accessor using the related_name attribute:

author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name=’books’)

Then you’d use:

author.books.all()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does enumerate() do?

A

It’s a built in Python function that takes a collection (eg. A tuple) and returns it as an enumerate object.

Eg. Given

x = (‘item1’, ‘item2’, ‘item3’)

y = enumerate(x)

Print(list(y))

[(0, ‘item1’), (1, item2), (2, item3)]

It adds a counter as the key of the function - enumerate(iterable, start)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly