Python | Django Models

Last Updated :
Discuss
Comments

Question 1

What defines a Django model, and which base class must it inherit from to work with Django’s ORM?

  • A Python dictionary inheriting from dict

  • A Python class that inherits from models.Model to represent a database table

  • A JSON configuration file used for schema settings

  • An SQL script template that extends QuerySet

Question 2

What happens when python manage.py makemigrations is run after adding a new field to a model, and how is this different from running the migrate command?

  • It applies SQL changes directly to the database while migrate creates Python files for schema tracking

  • It only checks field types without creating any files while migrate resolves app dependencies

  • It creates Python migration files that record the schema change while migrate applies those migration files to the database

  • It compiles model code into bytecode while migrate runs tests related to schema updates

Question 3

After defining a model and applying migrations, what can be done next to ensure the model is functioning correctly with the database?

  • Modify static files to check if the model reconnects automatically

  • Clear browser cache to refresh model behavior

  • Restart the system to reload all model definitions

  • Use Django’s ORM to interact with the model, such as creating or retrieving records in the shell

Question 4

In a user profile model, which field type stores short URL-friendly text such as a username, automatically validating characters to ensure a proper slug format?

  • CharField(max_length=50) – Basic text storage without slug rules

  • EmailField(max_length=254) – Validates email format only

  • SlugField(max_length=50) – Stores URL-friendly text with built-in slug validation

  • TextField() – Used for long text without restrictions

Question 5

Which Django field type is used when a model needs to store only non-negative whole numbers and must prevent any negative values?

  • PositiveIntegerField() – Stores integers that are zero or greater

  • BigIntegerField() – Supports very large signed integers, including negatives

  • SmallIntegerField() – Allows both positive and negative small integers

  • DecimalField() – Stores decimal values with fixed precision

Question 6

Which Django field stores both date and time for event logging, supports auto-setting the value only when the record is created, and internally uses Python’s datetime.datetime type?

  • DateField(auto_now_add=True) – Stores only the date using datetime.date

  • TimeField(auto_now_add=True) – Stores only the time using datetime.time

  • DurationField(auto_now_add=True) – Stores time intervals using datetime.timedelta

  • DateTimeField(auto_now_add=True) – Stores full date and time using datetime.datetime

Question 7

For storing user avatar images in a model, which Django field handles image uploads, performs automatic image-type validation using Pillow, and builds on the functionality of FileField?

  • ImageField(upload_to='avatars/') – Uploads and validates image files using Pillow

  • FileField(upload_to='avatars/') – Accepts general files without image validation

  • BinaryField() – Stores raw binary data, not intended for file uploads

  • URLField() – Stores links, not uploaded files

Question 8

In a Django model field, what does blank=True allow compared to null=True, and at which validation levels do they mainly apply?

  • blank=True allows empty values during form validation and null=True allows storing NULL in the database working at form level and database level

  • blank=True forces uniqueness and null=True sets default values working during migrations

  • blank=True stores empty values as NULL and null=True hides the field from forms working only at the database level

  • blank=True enables editing and null=True creates indexes working only in the admin interface

Question 9

For a status field that uses default='draft' together with choices=[('draft', 'Draft'), ('published', 'Published')], what happens when a new instance is created without providing a value?

  • It always selects the first choice and does not use the default

  • It requires the default to be a callable and applies choices only after saving

  • It adds the value draft to the choices list dynamically and skips validation

  • It assigns draft if it exists in the choices list and raises an error only if the default is not a valid choice

Question 10

When unique=True is set on a model field, what happens during save, and how is this different from the database enforcing uniqueness?

  • It raises an IntegrityError if a duplicate is saved and the database also enforces this through a unique constraint created during migrations

  • It checks only during form submission and the database still allows duplicates if a rollback occurs

  • It automatically generates a unique value and overwrites duplicates without errors

  • It applies only within the app and global uniqueness must be configured in settings.py

Tags:

There are 15 questions to complete.

Take a part in the ongoing discussion