Django

Django Models - Basics

Model fields, field types, Meta class, model methods, QuerySet basics, migrations

22 interview questions·
Junior
1

What is a Model in Django?

Answer

A Django Model is a Python class that represents a table in the database. Each Model attribute corresponds to a column in the table. Django uses ORM (Object-Relational Mapping) to automatically translate operations on Python objects into SQL queries, allowing database manipulation without writing SQL directly.

2

Which base class should be used to create a Django Model?

Answer

All Django Models must inherit from django.db.models.Model. This base class provides all the necessary functionality to interact with the database: table creation, queries, saving, deletion, etc. Without this inheritance, the class would not be recognized as a Model by Django.

3

Which field type should be used to store a string with a limited length?

Answer

CharField is used to store short to medium strings with a maximum length defined via max_length. This parameter is required for CharField. For longer texts without size limits, TextField should be used instead as it has no length restriction.

4

What is the difference between the null and blank options in a Django field?

5

How to define a default value for a Model field?

+19 interview questions

Master Django for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free