0
0
Djangoframework~10 mins

On_delete options (CASCADE, PROTECT, SET_NULL) 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 delete related objects automatically when the referenced object is deleted.

Django
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)
Drag options to blanks, or click blank then click option'
ACASCADE
BPROTECT
CSET_NULL
DDO_NOTHING
Attempts:
3 left
💡 Hint
Common Mistakes
Using PROTECT will prevent deletion instead of deleting related objects.
Using SET_NULL requires the field to allow null values.
2fill in blank
medium

Complete the code to prevent deletion of the referenced object if related objects exist.

Django
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)
Drag options to blanks, or click blank then click option'
ADO_NOTHING
BCASCADE
CSET_NULL
DPROTECT
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE deletes related objects instead of blocking deletion.
SET_NULL requires nullable fields and does not block deletion.
3fill in blank
hard

Fix the error in the code to allow setting the foreign key to null when the referenced object is deleted.

Django
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)
Drag options to blanks, or click blank then click option'
ASET_NULL
BCASCADE
CPROTECT
DDO_NOTHING
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE deletes the related object instead of setting null.
PROTECT prevents deletion and does not set null.
4fill in blank
hard

Fill both blanks to create a foreign key that sets the field to null on deletion and allows null values.

Django
class Article(models.Model):
    author = models.ForeignKey(User, on_delete=models.[1], [2]=True)
    headline = models.CharField(max_length=255)
Drag options to blanks, or click blank then click option'
ASET_NULL
Bnull
Cblank
DCASCADE
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE deletes the article instead of setting author to null.
Using blank=True does not allow null values in the database.
5fill in blank
hard

Fill all three blanks to define a foreign key that protects deletion, allows blank input, and does not allow null values.

Django
class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.[1], [2]=False, [3]=True)
    content = models.TextField()
Drag options to blanks, or click blank then click option'
APROTECT
Bnull
Cblank
DCASCADE
Attempts:
3 left
💡 Hint
Common Mistakes
Using CASCADE deletes comments when post is deleted.
Setting null=True allows null values which is not desired here.