Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🟣 Django interview questions and answers to help you prepare for your next technical interview in 2025.

NotificationsYou must be signed in to change notification settings

Devinterview-io/django-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

web-and-mobile-development

You can also find all 70 answers here 👉Devinterview.io - Django


1. What isDjango and what are its key features?

Django is a high-level Python web framework celebrated for its emphasis on simplifying complex tasks and following the "Don't Repeat Yourself" (DRY) and "model-template-views" (MTV) paradigms. It's renowned for rapid development, robustness, and vast ecosystem of packages.

Key Features

  • Object-Relational Mapping : Enables database interactions using Python objects.
  • Admin Panel : Generates a user-friendly interface for database management.
  • URL Mapping : Routes web requests based on URLs, usingurls.py files.
  • Template Engine : Processes HTML templates, separating design from logic.
  • Form Handling : Simplifies form validation and rendering.
  • Security : Offers built-in protections against common web vulnerabilities.
  • Middleware : Allows global request/response customization.
  • Simplified Queries : Provides a high-level query API for database operations.
  • Shared Components : Supports pluggable apps for easy component sharing.
  • Auto-Documentation : Generates documentation for models and their attributes.
  • File Handling : Provides utilities for file uploads and serving.
  • Asynchronous Support : Enhanced in recent versions to handle asynchronous tasks.
  • Versatility : Compatible with various web servers, databases, and front-end frameworks.
  • Scalability : Adaptable to large, high-traffic projects.
  • Package Ecosystem : A rich collection of "pypi" packages complements Django's core features.
  • Built-in Cache : Offers caching support for performance optimization.
  • Internationalization : Facilitates multi-language support.
  • REST Framework : Offers extensive support for building RESTful APIs.

2. Explain theMTV (Model-Template-View) architecture pattern inDjango.

Django is built around the MVT (Model-View-Template), which is nearly identical to the more commonly knownMVC (Model-View-Controller) pattern. Here’s a breakdown of the core components in Django's MVT:

MVT Components

  • Model (MVC equivalent): Responsible for data access and business logic. A model in Django is typically a Python class that represents a database table.

  • View (MVC equivalent: Responsible for presenting data): Handles user input, processes requests, and returns appropriate responses. In newer versions of Django, the view is more akin to a controller and is responsible for the logical flow of the application.

  • Template (MVC equivalent): Responsible for the presentation and user interface. A template in Django is an HTML file that utilizes its templating language to dynamically render data.

Request-Response Lifecycle

  1. Client Request: A user initiates an action, for example by clicking on a link in a web browser.
  2. URL Dispatcher: The URL dispatcher (in newer versions, the 'path' or 're_path' function) maps the incoming URL to a corresponding view.
  3. View Processing: The view performs any necessary logic, such as retrieving data from the database using models.
  4. Response Building: The view sends the data to a template for rendering and then returns an HTTP response to the client.
  5. Template Rendering: If a template is used, it processes the data and renders the HTML, which is then included in the response.

Relationship with MVC

  • Model: Adheres closely to the MVT and MVC paradigms, representing data and business rules.

  • View vs. Controller: In MVT, the view correlates more closely to the traditional concept of a controller. This is because it processes incoming requests, interacts with models as needed, and oversees the flow of the application.

  • Template vs. View: The MVT view is akin to the MVC view, responsible for presenting data to the user. The MVT view, however, also processes user requests, whereas the MVC view is more passive and simply displays data provided by the controller. The MVT template is analogous to the MVC view in that it focuses on the presentation layer.


3. What is aDjango project and how is it different from aDjango app?

Django Project is the high-level umbrella under which you build a web application. It encompasses multiple components such as settings, URLs, and can host several apps.

In contrast, aDjango App is a standalone module designed to serve a specific functionality or business area, following the concept of "application" as a group of related features.

Key Components of a Django Project

  • Settings: Configuration options for the project and its apps.
  • URLs: Defines the endpoint mappings usingurls.py.
  • WSGI/ASGI: Entry points for the web server to serve the project, handling HTTP and WebSocket requests, respectively.

Key Components of a Django App

  • Models: Data layer, defining data structures using models.py.
  • Views: Business logic and presentation layer.
  • Templates (optional): Presentation layer, housing HTML and rendering logic.
  • URLs: Endpoint mappings for the app, known as "scoped URLs".

Code Example: Project and App Structure

Here is the folder structure.

myproject/     # Django Project    manage.py  # Project Management Utility    myproject/ # Project Directory        settings.py        urls.py        wsgi.py        asgi.py    myapp1/    # Django App 1    myapp2/    # Django App 2    ...

It shows the typical structure where a project contains one or more apps.

4. Describe the purpose of thesettings.py file in aDjango project.

Thesettings.py file in a Django project is crucial for configuring the project, including its applications and external resources. It allows forcustomization andcontrol over various aspects of the Django project.

Key Features

  • Global Settings: Manages project-wide configurations such as installed applications, middleware, URLs, and more.

  • Dynamic Configuration: Utilizes environment variables to secure sensitive data and allows for different configurations in development, testing, and production environments.

  • Security and Debugging: Provides options for CSRF protection, session management, and detailed error handling.

  • Database Setup: Offers flexibility to work with different databases, including SQLite, MySQL, PostgreSQL, and others.

  • Internationalization and Localization: Facilitates multi-language support for web applications.

  • Customization and Extensibility: Supports third-party app integration and allows for the creation of customAppConfig classes.

Importance of settings.py

  • Centralized Configuration: Eliminates the need for scattered config files and ensures all settings are conveniently located.

  • Adaptability: Its modifiability caters to evolving project requirements and changing deployment environments.

  • Consistency: Promotes a uniform configuration setup across development, testing, and production stages.

  • Version Control: In multi-developer setups, tracking changes to this file ensures everyone is on the same configuration page.

Example Use-Cases

  1. Installed Applications:

    • Identifying project-specific apps for features such as authentication, REST APIs, etc.
  2. Middleware:

    • Global request/response handling, like CORS or authentication.
  3. Database Configuration:

    • Choosing the target database, setting up primary database connections.
  4. Static and Media Files:

    • Configuring file storage for user-uploaded content.
  5. Internationalization:

    • Enabling and configuring multi-language support.
  6. Security and Debugging:

    • Managing settings likeDEBUG mode, CORS policies, and SSL/HTTPS requirements.
  7. Custom AppConfigs:

    • Fine-tuning settings for specific Django apps or handling app initialization routines.
  8. Environmental Variables:

    • Using keys, secrets, or sensitive data from the environment for security and flexibility.
  9. Testing and CI:

    • Adjusting configurations for automated testing and continuous integration pipelines.

5. What is the role of theurls.py file in aDjango project?

In Django, theurls.py file plays a key role inrouting andmapping URLs to views. Each app typically has its ownurls.py for modular URL handling.

Global URLs vs. App URLs

  • Global (project.urls): The project's mainurls.py generally includes paths to various apps.

  • Local (app.urls): Each app'surls.py handles its specific URL mappings.

URL Patterns

Everyurls.py file uses URL patterns, defined withpath() orre_path().

  • path(): For simple text-based URLs.
  • re_path(): For complex URLs using regular expressions.

Passing URLs to Views

Thepath() function'sview argument typically points to the corresponding view function. You can also sendadditional data, like query parameters or URL segments.

Example: path()

fromdjango.urlsimportpathfrom .importviewsurlpatterns= [path('articles/2023/',views.special_case_2003),]

In the example, the URL pattern directs any request to/articles/2023/ to thespecial_case_2003 view.

Example: path() with Parameters

fromdjango.urlsimportpathfrom .importviewsurlpatterns= [path('articles/<int:year>/',views.year_archive),]

Here, the URL pattern sends any matching URL (like/articles/2022/) and the extracted year to theyear_archive view.

URL Handling Best Practices

  • Consistency: Follow a clear and standardized URL structure.
  • Namespacing: Employ app namespaces to avoid URL name clashes.
  • Template Integration: Usereverse() andredirect() to decouple URLs from views.

6. Explain the concept ofDjango's ORM (Object-Relational Mapping).

Django's ORM bridges the gap between relational databases and Python objects. This integration allows you to perform database operations using high-level Pythonic methods, rather than direct SQL queries.

Core Components

The three fundamental components of Django's ORM are:

  1. Models: Defined inmodels.py, they specify the database structure in a class-based syntax.

  2. Model Instances: These are objects created from your models and represent specific database records.

  3. QuerySets: These are high-level, chainable, and lazy-evaluated database queries that allow you to interact with your model instances.

Key Concepts

  • Model Classes: These are Python classes that inherit fromdjango.db.models.Model. Each class attribute represents a database field.

  • Field Types: Django provides a range of field types (such asCharField,IntegerField, andForeignKey) to cover diverse data requirements.

  • Model Relationships: Models can be related to one another, for example, in a one-to-many or many-to-many configuration.

  • Manager Methods: Objects of themodels.Manager class provide utility functions for database access. Every model has at least one manager, typically namedobjects.

  • QuerySet Methods: Theobjects attribute of a model provides aQuerySet. This can be filtered, sorted, and manipulated in various ways before evaluation.

Benefits of Using ORM

  • Portability and Flexibility: Applications developed on top of the ORM can quickly adapt to different database backends.

  • Security: The ORM offers protections against common security threats like SQL injection.

  • Productivity: Higher abstraction levels reduce the need for intricate database-specific code, resulting in quicker development cycles.

Code Example: ORM in Action

Here is a simple model in Django:

fromdjango.dbimportmodelsclassAuthor(models.Model):name=models.CharField(max_length=100)classBook(models.Model):title=models.CharField(max_length=100)author=models.ForeignKey(Author,on_delete=models.CASCADE)published_date=models.DateField()@propertydefis_recent(self):returnself.published_date>some_logic_to_get_recent_date()# Querying examples:# Get all books published in the last year, written by authors with a name starting with 'A', and order them by title.recent_books=Book.objects.filter(published_date__gt=one_year_ago,author__name__istartswith='a').order_by('title')# Get the top 5 authors with the most books:top_authors=Author.objects.annotate(num_books=models.Count('book')).order_by('-num_books')[:5]

7. What is aDjango model and how is it defined?

Django Models form the architectural foundation for database-driven applications. A model serves as a data structure and includes essential fields and behaviors.

Model Definition

A model class is a Pythonsubclass ofdjango.db.models.Model. Each attribute within the class represents a database field.

The model class specifies the database table name, any data relationships, metadata, and methods for data manipulation.

Key Model Components

Fields

  • Definition: Represent the table's columns.
  • Types: e.g.,CharField,DateField.
  • Primary Key:id is default or can be customized.
  • Descriptors: Define field attributes e.g.,null,blank.

Meta Options

  • table_name: Database table name.
  • unique_together: A list of tuples for fields that should be unique when considered together.
  • ordering: Default result order.

Methods

  • Objects: Custom data managers.
  • __str__(): Human-readable instance name for admin and more.

Example: Model Code

Here is the Django code for theAuthor andBook model which demonstrates relationships between the two models:

fromdjango.dbimportmodelsclassAuthor(models.Model):name=models.CharField(max_length=100)birth_date=models.DateField()def__str__(self):returnself.nameclassBook(models.Model):title=models.CharField(max_length=100)author=models.ForeignKey(Author,on_delete=models.CASCADE)published_date=models.DateField()genres=models.ManyToManyField('Genre')def__str__(self):returnself.titleclassGenre(models.Model):name=models.CharField(max_length=100)def__str__(self):returnself.name

In this example:

  • Author andBook are model classes, each with its own table and fields in the database.
  • Author has aManyToMany relationship withGenre through theBook model.
  • ForeignKey, linkingBook toAuthor, establishes a one-to-many relationship.

TheForeignKey indicates each Book relates to one Author, while the cascadeon_delete specifies that if an Author is deleted, all their books are also deleted.

8. Describe the purpose ofDjango's admin interface.

The Django admin interface is a powerful tool that automatically generates a user-friendly interface for performing common data management tasks, such as creating, reading, updating, and deleting (CRUD operations) for your application's models.

Key Features

  • Quick Implementation: Provides out-of-the-box tools for data management, requiring minimal setup.
  • Model-Centric Interface: Operations are organized based on your application's data models.
  • DRY (Don't Repeat Yourself) Philosophy: Changes to your models are automatically reflected in the admin interface.

Common Admin Actions

  • Database Records: View, add, edit, and delete records.
  • Data Relationships: Navigate through foreign key and many-to-many relationships.
  • Data Validation: Basic field-level validation is performed.

When to Use and When Not to Use the Admin Interface

When to Use

  • Rapid Prototyping: For quick feedback or proof of concept.
  • Administrative Tasks: For internal tools or during early project stages.
  • Quick Data Fixes: Ad-hoc data corrections or debugging.

Code Example: Setting Up the Admin Interface

Here is the Python code:

fromdjango.contribimportadminfrom .modelsimportMyModel@admin.register(MyModel)classMyModelAdmin(admin.ModelAdmin):list_display= ('field1','field2','some_method','related_field__nested_field')list_filter= ('field1','field2')search_fields= ('field1','field2','related_field__nested_field__name')defsome_method(self,obj):returnobj.field1+" - "+obj.field2some_method.short_description='Custom Description'

When Not to Use

  • Production Client-Facing UI: The admin interface is not designed for public, customer-facing views. It's preferable to create custom views using Django's forms and templating for public consumption.
  • Complex Data Operations: For intricate data management or workflows, a custom UI provides better control and user experience.

Admin Interface in urls.py

Here is the Python code:

fromdjango.contribimportadminfromdjango.urlsimportpathurlpatterns= [path('admin/',admin.site.urls),# ...]

9. What is aDjango view and how is it created?

ADjango view functions as an endpoint for web requests. It retrieves data from a database, processes it as needed, and then returns a response, which can be an HTML page, a redirect, or a JSON object, among others.

Types of Views

  • Function-Based Views (FBV): These are created using functions.
  • Class-Based Views (CBV): These are created using classes and offer a more structured approach, often with built-in features like HTTP method handling and view mixins.

Steps to Create a Django View

  1. Define the View Function or Class: This entails specifying the unique logic for the view. In the case of a function-based view, it's a Python function with the@ decorator and an HTTP method. For a class-based view, you define a class with specific method names for different HTTP methods.

  2. Map the View to a URL: Every view must be associated with a URL pattern to be accessible. This is orchestrated in theurls.py file. You can use eitherpath orre_path for simple or advanced URL matching, respectively.

  3. Handle the Request and Generate a Response: In the view, the HTTP request is received and parsed as needed. The response should adhere to the view's requirements, which could mean rendering a template, redirecting the user, or returning specific data types (e.g., JSON).

Code Example: Function-Based View

Here is the Django view-specific code:

# views.pyfromdjango.httpimportHttpResponsefromdjango.shortcutsimportrenderdefmy_view(request):# Business logic, such as database queries or form processingdata= ...# Return a rendered template or a custom HTTP responsereturnrender(request,'template.html', {'data':data})

Code Example: Class-Based View

Here is the Django view-specific code:

# views.pyfromdjango.viewsimportViewfromdjango.httpimportJsonResponseclassJSONResponseView(View):defget(self,request):returnJsonResponse({'key':'value'})defpost(self,request):returnJsonResponse({'key':request.POST['data']})

Routing and URL configuration

In a DjangoMVC (Model-View-Template) setup, theURL configuration functions as a mediator between the view and the model, handling incoming HTTP requests and ensuring an appropriate view responds.

In more complex web applications, this strategy allows for a clear separation of concerns, making both maintenance and expansion more straightforward.

10. Explain the concept ofURL patterns inDjango.

In Django,URL patterns direct web requests to the appropriate view for processing. They are defined in theurls.py file of each app and can be simple strings or regular expressions.

Basic URL Patterns

  • Basic Syntax: A URL pattern is a string matching the incoming request's path. For a match to occur, the URL pattern fromurls.py must be identical to the request path.

  • Example:

    fromdjango.urlsimportpathfrom .importviewsurlpatterns= [path('articles/2022/',views.article_2022),]

    In this example, only requests to/articles/2022/ will match.

Regular Expressions for Advanced URL Patterns

For more advanced matching, Django allows the use of regular expressions.

  • Syntax: Such patterns open with^ and end with$ to indicate the full path. This syntax gives flexibility in pattern matching.

  • Example:

    fromdjango.urlsimportre_pathfrom .importviewsurlpatterns= [re_path(r'articles/(\d{4})/',views.article_year),]

    Here, requests to paths like/articles/2022/ will match, and the year part will be captured and sent to the associated view.

URL Pattern Helpers

Django provides several helper functions to streamline URL pattern definitions:

  • path(): For simple matching based on the path.
  • re_path(): For using regular expressions.
  • include(): Enables URL grouping and delegation to other URL config files.

Best Practices

Keep URLs Simple

For many projects, basic string matching inpath() functions suffices. This approach enhances readability and maintainability.

Use Unambiguous URL Designs

Clear URL designs improve both developer and user experiences.

Leverage Named URLs

Assigning names to URLs viapath() andre_path() aids in referencing URLs in templates, keeping code DRY (Don't Repeat Yourself).

11. What is adatabase migration inDjango and why is it important?

Database migrations in Django are changes to the database schema, ensuring it stays in sync with the evolving data models. Each migration is represented as aPython file, making alterations via thedjango.db.migrations module.

Key Components

  • Migration Files: These are generated sequentially and outline the changes to be applied to the database.
  • Migrations Modules: A collection of related migration files.
  • Migration Plan: A record of applied and unapplied migrations.

Why Use Migrations?

  • Version Control: Migrations are text-based, making them ideal for version control systems like Git.
  • Data Integrity: They help ensure that data remains consistent as the schema evolves.
  • Collaboration: Simplifies team collaboration and deployment processes.
  • Adaptability: Migrations can be tailored for different databases, such as PostgreSQL or MySQL.

Basic Migration Commands

  1. Initial Migration: Generate the initial database schema from models.

    python manage.py makemigrations
  2. Apply Migrations: Execute the pending migrations on the database.

    python manage.py migrate
  3. Migrations Plan: Check the status of migrations.

    python manage.py showmigrations

Advanced developers may often require more intricate migration commands and functionalities. Fortuitously, Django offers a versatile array of options to accommodate those needs.

12. Explain the difference between aForeignKey and aManyToManyField inDjango models.

In Django models, relationships can be established using either aForeignKey or aManyToManyField, each serving a unique purpose.

ForeignKey

AForeignKey sets up amany-to-one relationship. It's used when each record in the current model needs to be associated with exactly one record in another model.

Example:

  • ABook has onePublisher.
  • This is represented by a ForeignKey in theBook model pointing to thePublisher model.

Code Example (ForeignKey):

fromdjango.dbimportmodelsclassPublisher(models.Model):name=models.CharField(max_length=100)classBook(models.Model):title=models.CharField(max_length=100)publisher=models.ForeignKey(Publisher,on_delete=models.CASCADE)

ManyToManyField

AManyToManyField is used when a record in one model can be associated with multiple records in another, and vice versa.

Many-to-Many Relationship

It facilitates amany-to-many relationship where multiple records in one model can be linked to multiple records in another.

Example:

  • AStudent can enroll in multipleCourses, and aCourse can have multiple students enrolled.
  • This many-to-many relationship is represented using a ManyToManyField with an intermediary table.

Code Example (ManyToManyField):

fromdjango.dbimportmodelsclassCourse(models.Model):name=models.CharField(max_length=100)classStudent(models.Model):name=models.CharField(max_length=100)courses=models.ManyToManyField(Course)

ManyToManyField with Additional Fields (through):

Sometimes, the relationship between the two models needs to have additional fields.ManyToManyField allows you to specify a custom intermediary model using thethrough parameter.

Here is an example:

You have a Music Library and want to associateSongs withPlaylists, but you also want to track the order of the songs in each playlist.

You would use a model likePlaylistSong as the intermediary model.

classPlaylist(models.Model):name=models.CharField(max_length=100)classSong(models.Model):title=models.CharField(max_length=100)playlists=models.ManyToManyField(Playlist,through='PlaylistSong')classPlaylistSong(models.Model):song=models.ForeignKey(Song,on_delete=models.CASCADE)playlist=models.ForeignKey(Playlist,on_delete=models.CASCADE)order=models.IntegerField()# Possibly other fields like 'added_by' and 'date_added'

Here, thePlaylistSong model contains the additionalorder field to track the song's order in the playlist.

13. How do you define a custommodel field inDjango?

In Django, you can define a custom model field to encapsulate complex data types, enforce specific behavior, or integrate with external data sources.

Custom Model Field Definition

Here is the Python code:

fromdjango.dbimportmodelsfromdjango.core.exceptionsimportValidationErrorclassMyCustomField(models.Field):description="A custom field for special data handling."def__init__(self,*args,**kwargs):# Initialize your custom field attributes heresuper().__init__(*args,**kwargs)defdb_type(self,connection):# Define the database column typereturn'SOME_DB_TYPE'deffrom_db_value(self,value,expression,connection):# Convert the database value to the expected Python objectifvalueisnotNone:# Perform any necessary data transformations or validationsreturntransformed_valuereturnvaluedefto_python(self,value):# Convert the value to the appropriate Python object typeifisinstance(value,str):returnparse_string_value(value)returnvaluedefget_prep_value(self,value):# Convert the provided Python value for storage in the databasereturnformatted_valuedefvalidate(self,value,model_instance):# Implement custom data validationsifnotis_valid:raiseValidationError("Invalid data.")defformfield(self,**kwargs):# Customize the form field for this model fielddefaults= {'form_class':CustomFormFieldClass}defaults.update(kwargs)returnsuper().formfield(**defaults)

Field Methods Overview

  • __init__: Initializes custom field attributes.
  • db_type: Specifies the database column type.
  • from_db_value: Converts database value to Python object.
  • to_python: Specifies the Python data type.
  • get_prep_value: Converts value for storage in the database.
  • validate: Provides custom data validations.
  • formfield: Customizes the form field for the field type.

Use Cases for Custom Fields

  1. Encapsulating Legacy Data: Handle complex or legacy data formats transparently.
  2. External Data Integration: Connect to external data sources or APIs for specific data requirements.
  3. Advanced Data Validation: Implement custom or advanced data validation rules beyond whatvalidators orclean methods offer.
  4. Specialized Data Types: Manage special data types or structures not covered by standard Django fields.
  5. Embedding Business Logic: Integrate data-specific business logic to be carried out at the model or form level.

14. What is aQuerySet inDjango and how is it used?

In Django, aQuerySet represents a collection ofdatabase queries that can bechained and lazily executed. It provides an intuitive way to retrieve and manipulate data from the database using Python.

QuerySet Basics

  • Initializationic: QuerySets are generated automatically when you interact with a Django model using its Manager.

  • Chaining: Multiple methods can be chained together before the QuerySet is evaluated.

  • Lazy Evaluation: QuerySets are only executed when data is required, such as when iterating through the results or explicitly calling evaluation methods likelist() orcount().

  • Immutability After Evaluation: Once a QuerySet is evaluated, its results cannot be altered. For instance, you can't add more items to a list after it's been created.

QuerySet Methods

Data Retrieval

  • all(): Returns all objects in the QuerySet.
  • get(): Retrieves a single object that matches the provided criteria.
  • filter(): Returns a subset of objects that match specific criteria.
  • exclude(): Returns objects that don't match the specified criteria.

Data Manipulation

  • create(): Instantly creates a new object and saves it to the database.
  • update(): Modifies objects in the database that match the given criteria.

Relationship Handling

  • select_related(): Fetches related Many-To-One objects. Efficient for queries with a small number of related objects.
  • prefetch_related(): Fetches the related objects for a Many-To-Many or reverse ForeignKey relationship.

QuerySet Execution

  • iterator(): Fetches objects from the database one at a time, useful for large result sets.
  • get_or_create(): Attempts to fetch an object from the database based on certain criteria and creates it if it doesn't exist.
  • earliest() and latest(): Retrieve the first and last object, respectively, based on the specified field.

Aggregation and Metrics

  • aggregate(): Performs an aggregate function (e.g., Count, Sum, Avg) over the items in the QuerySet.
  • count(): Returns the number of items in the QuerySet.

QuerySet Evaluation

  • bool(): ReturnsTrue if the QuerySet contains any results,False otherwise.
  • exists(): Checks if there are any results that match the query.

QuerySet Execution

  • order_by(): Orders the QuerySet results based on the specified field.
  • reverse(): Reverses the original ordering of the QuerySet.

Slicing and Pagination

  • iterator(): Fetches objects from the database one at a time, useful for large result sets.
  • first() and last(): Retrieve the first or last object from the QuerySet.
  • count(): Returns the number of items in the QuerySet.

QuerySet Convenience Methods

  • in_bulk(): Returns a dictionary of objects with their primary keys as the keys.
  • values() and values_list(): Return dictionaries or tuples, respectively, representing the objects' fields.
  • distinct(): Removes duplicate results from the QuerySet.
  • union(): Combines two or more QuerySets into a single, unique QuerySet.

15. Describe the concept ofmodel inheritance inDjango.

Model inheritance in Django allows you to create new models based on existing ones, leading to efficient code reuse and structured data management.

Model Inheritance Types

  1. Abstract Base Classes Inheritance: The parent model (defined asabstract) serves purely as a template and isn't used to create any database tables on its own.

  2. Multi-Table Inheritance: This approach createsseparate database tables for the parent model and each of its child models, maintaining a one-to-one relationship.

Common Inheritance Patterns

  1. STI - Single Table Inheritance
  2. MTI - Multi-Table Inheritance
  3. Concrete Classes vs. Abstract Base Classes

Inheritance in Relational Databases

In relational databases, inheritance is handled via table relationships. Django supports both the STI and MTI strategies.

Code Example: Abstract Base Class

Here is the Django code:

fromdjango.dbimportmodelsclassCommonInfo(models.Model):# This acts as an abstract base class.name=models.CharField(max_length=100)age=models.PositiveIntegerField()classMeta:# Set 'abstract' to True to make this an abstract base class.abstract=TrueclassStudent(CommonInfo):# Inherits fields 'name' and 'age' from CommonInfo.student_id=models.CharField(primary_key=True,max_length=10)

TheCommonInfo model here is anabstract base class. It's not directly instantiated in the database but provides fields likename andage for any model that inherits from it.

TheStudent model inherits fromCommonInfo and therefore shares its fields in addition to its own, likestudent_id.

The generated SQL forStudent will include fieldsname,age, andstudent_id.

Code Example: Multi-Table Inheritance

Here is the Django code:

fromdjango.dbimportmodelsclassPlace(models.Model):name=models.CharField(max_length=50)address=models.CharField(max_length=80)classRestaurant(Place):serves_hot_dogs=models.BooleanField(default=False)serves_pizza=models.BooleanField(default=False)

This results in two tables: one forPlace with fieldsname andaddress, and another forRestaurant with additional fieldsserves_hot_dogs andserves_pizza. TheRestaurant table has aone-to-one relationship with thePlace table.

Explore all 70 answers here 👉Devinterview.io - Django


web-and-mobile-development


[8]ページ先頭

©2009-2025 Movatter.jp