Complete the code to delete related objects automatically when the referenced object is deleted.
from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): author = models.ForeignKey(Author, on_delete=models.[1]) title = models.CharField(max_length=200)
Using CASCADE deletes all related Book objects when an Author is deleted.
Complete the code to prevent deletion of the referenced object if related objects exist.
from django.db import models class Category(models.Model): name = models.CharField(max_length=100) class Product(models.Model): category = models.ForeignKey(Category, on_delete=models.[1]) name = models.CharField(max_length=200)
Using PROTECT stops deletion of a Category if any Product refers to it.
Fix the error in the code to allow setting the foreign key to null when the referenced object is deleted.
from django.db import models class Publisher(models.Model): name = models.CharField(max_length=100) class Magazine(models.Model): publisher = models.ForeignKey(Publisher, on_delete=models.[1], null=True) title = models.CharField(max_length=200)
SET_NULL allows the publisher field to become null when the Publisher is deleted. The field must have null=True.
Fill both blanks to create a foreign key that sets the field to null on deletion and allows null values.
class Article(models.Model): author = models.ForeignKey(User, on_delete=models.[1], [2]=True) headline = models.CharField(max_length=255)
Use SET_NULL for on_delete and null=True to allow the field to be empty when the related User is deleted.
Fill all three blanks to define a foreign key that protects deletion, allows blank input, and does not allow null values.
class Comment(models.Model): post = models.ForeignKey(Post, on_delete=models.[1], [2]=False, [3]=True) content = models.TextField()
Use PROTECT to prevent deletion of Post if comments exist, null=False to disallow nulls, and blank=True to allow empty input in forms.