Recall & Review
beginner
What is a primary key in Django models?
A primary key is a unique identifier for each record in a database table. In Django, it uniquely identifies each model instance.
Click to reveal answer
beginner
What happens if you don't define a primary key in a Django model?
Django automatically adds an
id field as an auto-incrementing primary key.Click to reveal answer
intermediate
Can you use a field other than
id as a primary key in Django?Yes, you can set any field as the primary key by adding
primary_key=True to that field.Click to reveal answer
intermediate
What is the behavior of a primary key field with
primary_key=True in Django?It becomes unique, non-nullable, and automatically used as the identifier for model instances.
Click to reveal answer
beginner
Why should a primary key be unique and non-null in Django models?
Because it identifies each record uniquely and must always have a value to avoid confusion in data retrieval.
Click to reveal answer
What does Django do if no primary key is defined in a model?
✗ Incorrect
Django automatically adds an
id field as the primary key if none is defined.Which of these is true about a primary key field in Django?
✗ Incorrect
Primary keys must be unique and non-null to identify records uniquely.
How do you set a custom field as a primary key in Django?
✗ Incorrect
Setting
primary_key=True on a field makes it the primary key.What type of field does Django use by default for the primary key?
✗ Incorrect
Django uses an
AutoField, an integer that auto-increments, as the default primary key.Why is it important that a primary key is non-null?
✗ Incorrect
A non-null primary key ensures each record is uniquely identifiable.
Explain what a primary key is in Django and how Django handles it if you don't define one.
Think about what Django adds automatically to every model.
You got /4 concepts.
Describe how to set a custom primary key in a Django model and what changes this causes.
Focus on the field attribute that changes primary key behavior.
You got /3 concepts.