0
0
Djangoframework~10 mins

Primary key behavior in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a primary key field in a Django model.

Django
class Product(models.Model):
    id = models.[1](primary_key=True)
Drag options to blanks, or click blank then click option'
ACharField
BTextField
CForeignKey
DAutoField
Attempts:
3 left
💡 Hint
Common Mistakes
Using CharField or TextField as primary key without unique constraints.
Using ForeignKey instead of a primary key field.
2fill in blank
medium

Complete the code to set a custom field as the primary key in a Django model.

Django
class Employee(models.Model):
    employee_id = models.[1](primary_key=True, max_length=10)
Drag options to blanks, or click blank then click option'
AIntegerField
BCharField
CDateField
DBooleanField
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerField without auto-increment for string IDs.
Forgetting to set primary_key=True.
3fill in blank
hard

Fix the error in the model definition to correctly define a primary key.

Django
class Order(models.Model):
    order_number = models.IntegerField()
    [1] = models.AutoField(primary_key=True)
Drag options to blanks, or click blank then click option'
Apk
Border_id
Cid
Dorder_number
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for two fields causes errors.
Not defining a primary key when needed.
4fill in blank
hard

Fill both blanks to create a model with a custom primary key and a regular field.

Django
class Category(models.Model):
    [1] = models.[2](primary_key=True, max_length=20)
    name = models.CharField(max_length=100)
Drag options to blanks, or click blank then click option'
Acode
BCharField
CIntegerField
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using id when a custom key is intended.
Using IntegerField for string keys.
5fill in blank
hard

Fill all three blanks to create a model with an auto-increment primary key, a foreign key, and a text field.

Django
class Comment(models.Model):
    [1] = models.[2](primary_key=True)
    post = models.[3]('Post', on_delete=models.CASCADE)
    content = models.TextField()
Drag options to blanks, or click blank then click option'
Aid
BAutoField
CForeignKey
Dcomment_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field types for primary key or foreign key.
Forgetting to set primary_key=True on the key field.