0
0
Djangoframework~20 mins

Primary key behavior in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Primary Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the primary key value after saving a new Django model instance?

Consider this Django model and code snippet:

class Book(models.Model):
    title = models.CharField(max_length=100)

book = Book(title='Django Basics')
book.save()
print(book.pk)

What will print(book.pk) output?

Django
class Book(models.Model):
    title = models.CharField(max_length=100)

book = Book(title='Django Basics')
book.save()
print(book.pk)
AThe integer ID assigned by the database after saving, e.g., 1
BNone, because the primary key is not set until manually assigned
CRaises an AttributeError because pk is not defined
DThe title string 'Django Basics' as the primary key
Attempts:
2 left
💡 Hint

Think about what Django does when you save a new model instance without specifying a primary key.

📝 Syntax
intermediate
2:00remaining
Which model definition correctly sets a custom primary key in Django?

Choose the correct Django model code that defines a username field as the primary key.

A
class User(models.Model):
    username = models.CharField(max_length=30, primary_key=True)
    email = models.EmailField()
B
class User(models.Model):
    username = models.CharField(max_length=30)
    email = models.EmailField()
    primary_key = 'username'
C
class User(models.Model):
    username = models.CharField(max_length=30)
    email = models.EmailField()

    class Meta:
        primary_key = 'username'
D
class User(models.Model):
    username = models.CharField(max_length=30, unique=True)
    email = models.EmailField()
Attempts:
2 left
💡 Hint

Remember how to mark a field as primary key in Django model fields.

🔧 Debug
advanced
2:30remaining
Does this Django model raise an error on migration?

Given this model:

class Product(models.Model):
    sku = models.CharField(max_length=20, primary_key=True)
    name = models.CharField(max_length=100)

class Order(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.IntegerField()

Running migrations: does it raise an error about missing primary key?

Django
class Product(models.Model):
    sku = models.CharField(max_length=20, primary_key=True)
    name = models.CharField(max_length=100)

class Order(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.IntegerField()
AForeignKey must reference a field named 'id' by default, so referencing 'sku' causes error
BThe Product model must have an explicit 'id' field even if 'sku' is primary key
CNo error occurs; the code is valid and migrations run fine
DThe ForeignKey needs to specify 'to_field="sku"' to link to the custom primary key
Attempts:
2 left
💡 Hint

Think about how ForeignKey determines which field to reference on the target model when it has a custom primary key.

state_output
advanced
2:00remaining
What is the value of the primary key after updating a Django model instance?

Given this code:

class Customer(models.Model):
    email = models.EmailField(primary_key=True)
    name = models.CharField(max_length=50)

c = Customer(email='a@example.com', name='Alice')
c.save()
c.name = 'Alice Smith'
c.save()
print(c.pk)

What will print(c.pk) output?

Django
class Customer(models.Model):
    email = models.EmailField(primary_key=True)
    name = models.CharField(max_length=50)

c = Customer(email='a@example.com', name='Alice')
c.save()
c.name = 'Alice Smith'
c.save()
print(c.pk)
ANone, because updating clears the primary key
B'a@example.com', the primary key remains the same after update
CRaises a ValueError because primary key cannot be updated
DThe name 'Alice Smith' because primary key changes to last saved field
Attempts:
2 left
💡 Hint

Think about whether the primary key changes when you update other fields.

🧠 Conceptual
expert
3:00remaining
What happens if you define two fields with primary_key=True in a Django model?

Consider this model:

class Employee(models.Model):
    emp_id = models.IntegerField(primary_key=True)
    badge_id = models.CharField(max_length=10, primary_key=True)
    name = models.CharField(max_length=50)

What will happen when you try to run migrations?

Django
class Employee(models.Model):
    emp_id = models.IntegerField(primary_key=True)
    badge_id = models.CharField(max_length=10, primary_key=True)
    name = models.CharField(max_length=50)
AMigrations succeed but database throws an error at runtime
BCreates a composite primary key with both fields combined
COnly the first primary_key=True field is used; the second is ignored
DRaises a SystemCheckError because Django models cannot have multiple primary keys
Attempts:
2 left
💡 Hint

Recall Django's rules about primary keys in models.