
Django Models - Basics
Model fields, field types, Meta class, model methods, QuerySet basics, migrations
1What is a Model in Django?
What is a Model in Django?
回答
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.
2Which base class should be used to create a Django Model?
Which base class should be used to create a Django Model?
回答
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.
3Which field type should be used to store a string with a limited length?
Which field type should be used to store a string with a limited length?
回答
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.
What is the difference between the null and blank options in a Django field?
How to define a default value for a Model field?
+19 面接問題