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?
class Book(models.Model): title = models.CharField(max_length=100) book = Book(title='Django Basics') book.save() print(book.pk)
Think about what Django does when you save a new model instance without specifying a primary key.
Django automatically creates an integer primary key field called id if none is specified. When you save a new instance, the database assigns this ID, which is accessible via pk.
Choose the correct Django model code that defines a username field as the primary key.
Remember how to mark a field as primary key in Django model fields.
To set a custom primary key, you add primary_key=True to the field definition. Options B and C are invalid syntax, and D only makes the field unique but not a primary key.
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?
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()
Think about how ForeignKey determines which field to reference on the target model when it has a custom primary key.
No error occurs; the code is valid and migrations run fine. Django's ForeignKey automatically references the primary key field of the target model ('sku'), regardless of name. to_field is only required when referencing a non-primary-key field.
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?
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)
Think about whether the primary key changes when you update other fields.
The primary key value remains the same unless explicitly changed. Updating other fields and saving does not affect the primary key.
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?
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)
Recall Django's rules about primary keys in models.
Django does not support multiple primary keys on a model. Defining more than one field with primary_key=True causes a system check error during migration creation.