DEV Community

Vincent Tommi
Vincent Tommi

Posted on

Django Interview Questions & Key Concepts โ€“ Part 6

1 ๐Ÿ’พ Which Databases Does Django Support?
Django supports several databases out of the box, making it easy to integrate with popular systems depending on your project's needs.

โœ… Officially Supported Databases
Django has built-in support for:

๐Ÿงฉ SQLite โ€“ Great for development and small projects. No setup needed.
(Simple and fast, perfect for testing or prototypes)

๐Ÿ˜ PostgreSQL โ€“ Djangoโ€™s most feature-rich option, ideal for production apps.
ENGINE: 'django.db.backends.postgresql'
(Supports advanced features like full-text search, JSON fields, GIS, etc.)

๐Ÿฌ MySQL โ€“ Popular for web apps, widely used in many environments.
ENGINE: 'django.db.backends.mysql'
(Reliable and performant, works well for medium to large apps)

๐Ÿ›๏ธ Oracle โ€“ Used in large enterprise systems.
ENGINE: 'django.db.backends.oracle'
(Enterprise-grade, suitable for big corporations with Oracle infrastructure)

๐Ÿ”Œ Third-Party Database Support
You can also connect Django to other databases using external packages:

๐Ÿชต CockroachDB

๐Ÿ”ฅ Firebird

๐Ÿง  IBM DB2

๐Ÿงช SAP SQL Anywhere

๐Ÿ’ผ Microsoft SQL Server / Azure SQL

โ˜๏ธ Google Cloud Spanner

These databases are not officially supported by Django, but you can still integrate them using third-party backends.

๐Ÿฆ  What About MongoDB?
Django doesnโ€™t officially support MongoDB โŒ because it's a NoSQL/document-based database, while Djangoโ€™s ORM is designed for relational databases.
However, you can still use it through tools like:

๐Ÿ› ๏ธ Djongo โ€“ Allows Django ORM to work with MongoDB.

๐Ÿ”ง MongoEngine โ€“ ODM (Object-Document Mapper) designed for MongoDB.

โš ๏ธ Keep in mind: Using MongoDB with Django may limit some features that rely heavily on relational behavior.

๐Ÿงพ Summary
Django works best with relational databases like:

๐Ÿงฉ SQLite (lightweight, for dev)

๐Ÿ˜ PostgreSQL (most powerful & full-featured)

๐Ÿฌ MySQL (popular & scalable)

๐Ÿ›๏ธ Oracle (enterprise-level)

You can also extend Djangoโ€™s database support to other systems ๐Ÿงฉ using third-party tools.
โœ… Choose your database based on your appโ€™s size, feature needs, and performance requirements.

๐Ÿ› ๏ธ Djongo โ€“ Allows Django ORM to work with MongoDB.

๐Ÿ”ง MongoEngine โ€“ ODM (Object-Document Mapper) designed for MongoDB.

โš ๏ธ Keep in mind: Using MongoDB with Django may limit some
**
๐Ÿ” 2. How do you query all items from a database table in Django?**
To get all records from a database table in Django, use the all()method on a model's manager:

from .models import Car
all_objects = Car.objects.all()
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ This returns a QuerySet containing all Car objects from the database โ€” it's like saying "give me everything in the Car table."

๐Ÿ“ฆ This returns a QuerySet containing all Car objects from the database โ€” it's like saying "give me everything in the Car table."

๐Ÿงฐ Other Useful QuerySet Methods:
filter()โ€“ ๐Ÿ”Ž Retrieves objects that match specific criteria. Great for narrowing down your search.

values() โ€“ ๐Ÿ“‹ Returns dictionaries containing only the fields you specify instead of the full model objects.

๐Ÿ“Œ Example: Using values() to get only the firstname field from a model

from .models import Member
mydata = Member.objects.values('firstname')

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก This is useful when you only need certain pieces of information and want to reduce overhead in your queries.

3 Explain Django Response Life-cycle?
When a client ๐ŸŒโ€”such as a web browser or a mobile appโ€”sends a request to a Django application, the process begins with URL Routing ๐Ÿงญ. Django looks at the requested URL and compares it against the patterns defined in the urls.py file. If it finds a matching pattern, it directs the request to the corresponding view function. If no match is found, Django immediately returns a 404 Not Found error ๐Ÿšซ.

Before the view function is executed, the request goes through a series of middleware ๐Ÿ›ก๏ธ. Middleware are components that act like filters, sitting between the incoming request and the view logic. During this request phase, middleware can modify the request, enforce authentication ๐Ÿ”, log activity ๐Ÿ“Š, or even halt the request entirely and return a response early. They are useful for applying cross-cutting features across the whole application without writing repetitive code.

Once the request passes through the middleware, Django calls the view function or class-based view ๐Ÿง . This is where the main business logic of the application lives. The view might retrieve data from the database ๐Ÿ—ƒ๏ธ, handle user input, perform calculations, or prepare context data to be passed to a template. In some cases, the view may directly return an HttpResponse ๐Ÿ“ฆ, such as a plain text or JSON response for an API.

If the view is rendering an HTML page, it uses Djangoโ€™s template engine ๐Ÿ–ผ๏ธ. This engine takes a template file (which is just an HTML file with special Django tags) and combines it with dynamic data to produce the final HTML output. This allows the application to display dynamic contentโ€”like user information or database recordsโ€”embedded directly into the webpage ๐ŸŽจ.

After the view finishes its work, Django constructs an HttpResponse object ๐Ÿ“ฌ. This object includes the final content (like HTML or JSON), an HTTP status code (such as 200 OK โœ… or 403 Forbidden ๐Ÿšซ), and any necessary headers (like content type or cookies ๐Ÿช). This is the package that will eventually be delivered to the client.

Before being sent out, the response passes back through the middleware again ๐Ÿ”โ€”this time in reverse order. During this response phase, middleware can make final modifications such as setting cookies, compressing the response data ๐Ÿ“ฆ, adding custom HTTP headers, or logging response info. These tasks help improve security, performance, and debugging.

Finally, Django sends the response back to the client ๐Ÿ“ค. If the client is a browser, it renders the HTML and displays the webpage ๐Ÿ–ฅ๏ธ. If itโ€™s an API client, it might process the data further, such as rendering it in a dashboard or using it in a mobile app.

Throughout this process, Django relies on two main objects: the HttpRequest ๐Ÿ“ฅ, which holds details about the incoming request like the method, path, headers, and data; and the HttpResponse ๐Ÿ“ค, which carries the response content, status code, and headers back to the client. Understanding this lifecycle is essential for Django developers, as it helps with debugging, performance tuning, and extending application behavior with middleware or custom responses.

4 How to filter items in the Model?
In Django, when we want to retrieve specific records from the database, we use a QuerySet, which is essentially a collection of database queries built using Django's ORM (Object Relational Mapper). One of the most commonly used QuerySet methods is .filter(). This method allows us to filter data based on certain conditions, such as matching a field's value, checking if a field contains a substring, comparing numbers, or even filtering by dates.

๐Ÿ› ๏ธ The filter() Method
The .filter() method returns a new QuerySet containing only the rows that match the given condition(s). It doesn't modify the dataโ€”it just lets you query and retrieve exactly what you need.

๐ŸŒ Real-World Example
Letโ€™s say weโ€™re building an online bookstore and we have a Book model like this:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)
    genre = models.CharField(max_length=30)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    published_year = models.IntegerField()
Enter fullscreen mode Exit fullscreen mode

Now imagine our database has hundreds of books, but we only want to:
๐Ÿ“Œ Example 1: Get all books by a specific author

books = Book.objects.filter(author="J.K. Rowling")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” This will return a QuerySet of all books written by J.K. Rowling.

๐Ÿ“Œ Example 2: Get all books under $20

affordable_books = Book.objects.filter(price__lt=20.00)

Enter fullscreen mode Exit fullscreen mode

๐Ÿ” The __lt means "less than". You can also use__lte, __gt, __gte, etc.

๐Ÿ“Œ Example 3: Find books that contain a keyword in the title

keyword_books = Book.objects.filter(title__icontains="magic")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” __icontains allows case-insensitive searching for a substring in a field.

๐Ÿ“Œ Example 4: Get all fiction books published after 2015

recent_fiction = Book.objects.filter(genre="Fiction", published_year__gt=2015)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” You can combine multiple filters by separating them with commas.

๐Ÿง  Summary
Using .filter() is a powerful and readable way to retrieve only the data you need. Whether you're building a bookstore, blog, or social media app, QuerySets make it easy to query your data without writing raw SQL.

Top comments (0)