[{"id":"djangobeginner-001","courseId":"django-beginner","domain":"models-and-migrations","topic":"Models map to database tables","prompt":"You define a Person model in an app called myapp and set no table name anywhere. Which table does Django create for it?","hint":"The table name is built from metadata about the app and the model.","explain":"Each model is stored as a database table. Django builds that table's name automatically from where the model lives and what it is called, so you rarely need to write it yourself.","source":"https://docs.djangoproject.com/en/stable/topics/db/models/","interaction":{"type":"single"},"options":[{"id":"a","text":"myapp_person","why":"Django joins the app name and the lowercase model name with an underscore, giving myapp_person."},{"id":"b","text":"person","whyNot":"The default name keeps the app name in front, so the table is not named just after the model."},{"id":"c","text":"person_myapp","whyNot":"The app name comes first in the default table name, so the two parts are not reversed."},{"id":"d","text":"myapp_people","whyNot":"The default uses the model's own name, lowercased, not a plural form of it."}],"answer":"a","explanation":"Each model maps to a single table; by default its name is the app label plus the lowercase model name, joined by an underscore.","concept":"Each model maps to a single table; by default its name is the app label plus the lowercase model name, joined by an underscore.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"from django.db import models\n\nclass Person(models.Model):\n    name = models.CharField(max_length=30)","takeaways":["Each model maps to one table named app_model in lowercase."]},{"id":"djangobeginner-002","courseId":"django-beginner","domain":"models-and-migrations","topic":"Automatic primary key field","prompt":"On a model where you set primary_key=True on one of your own fields, Django does not add an automatic id column.","hint":"Think about what the primary_key option tells Django about the automatic column.","explain":"Every table identifies each row by a primary key. By default Django gives each model an auto-incrementing key, and you can choose one of your own fields to take that role instead.","source":"https://docs.djangoproject.com/en/stable/topics/db/models/#automatic-primary-key-fields","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"Setting primary_key=True is an explicit choice, so Django leaves out the automatic id column."},{"id":"b","text":"False","whyNot":"Django skips the automatic id when a field already sets primary_key=True, so the model does not get a second key."}],"answer":"a","explanation":"Setting primary_key=True on one of your own fields makes Django skip the automatic id column; otherwise Django adds an auto-incrementing id.","concept":"Setting primary_key=True on one of your own fields makes Django skip the automatic id column; otherwise Django adds an auto-incrementing id.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A field with primary_key=True stops Django from adding an automatic id."]},{"id":"djangobeginner-003","courseId":"django-beginner","domain":"models-and-migrations","topic":"null versus blank on fields","prompt":"You set null=True on a CharField but leave blank unset. Which TWO statements about these settings are correct?","hint":"One of these settings is about the database and the other is about forms.","explain":"Some field settings affect what gets saved in the database, while others affect what forms accept. Keep those two jobs separate when you read a field definition.","source":"https://docs.djangoproject.com/en/stable/ref/models/fields/#null","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"null=True makes Django store empty values as NULL in the database.","why":"The null option controls database storage: when True, empty values are stored as NULL."},{"id":"b","text":"Forms will still reject an empty value, because blank is False.","why":"With blank=False the field is required, so validation rejects an empty value in forms."},{"id":"c","text":"null=True lets empty values pass form validation.","whyNot":"That is the job of blank; null only affects database storage."},{"id":"d","text":"Django's convention is to store missing text as NULL, so null=True is the CharField default.","whyNot":"The convention for string fields is an empty string rather than NULL, and null defaults to False."},{"id":"e","text":"blank=True is what makes the database write NULL.","whyNot":"blank is validation-related; the database stores NULL because of null, not blank."},{"id":"f","text":"With null=True, a CharField can no longer hold an empty string.","whyNot":"A string field with null=True can still store an empty string, alongside NULL, as its no-data values."}],"answer":"ab","explanation":"null controls whether the database stores NULL for empty values; blank controls whether forms accept an empty value. The two are independent.","concept":"null controls whether the database stores NULL for empty values; blank controls whether forms accept an empty value. The two are independent.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["null is about storage; blank is about validation."]},{"id":"djangobeginner-004","courseId":"django-beginner","domain":"models-and-migrations","topic":"Callable field defaults","prompt":"A teammate passes a function to choices= on a CharField, so the options can change over time. What must that function look like?","hint":"Think about what Django needs from a callable when it reads the options.","explain":"A field's choices can be a fixed collection of value and label pairs. They can also be supplied as a callable, which suits options that may vary over time.","source":"https://docs.djangoproject.com/en/stable/ref/models/fields/#default","interaction":{"type":"single"},"options":[{"id":"a","text":"It takes no arguments and returns a mapping or a sequence of value and label pairs.","why":"A choices callable expects no arguments and returns the mapping or two-item pairs format."},{"id":"b","text":"It takes the model instance as its one argument and returns a list of choices.","whyNot":"A choices callable expects no arguments, so the model instance is not passed in."},{"id":"c","text":"It takes no arguments and returns the choices as bare strings with no labels.","whyNot":"Each choice needs a value and a human-readable name, so bare strings do not fit the format."},{"id":"d","text":"It takes no arguments and returns a single string used as the field's value.","whyNot":"A callable for choices returns the choice formats, not one value to store in the field."}],"answer":"a","explanation":"choices can be a callable that expects no arguments and returns a mapping or a sequence of two-item value and label pairs.","concept":"choices can be a callable that expects no arguments and returns a mapping or a sequence of two-item value and label pairs.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"def get_levels():\n    return {'FR': 'Freshman'}\n\nclass Student(models.Model):\n    level = models.CharField(\n        max_length=2, choices=get_levels)","takeaways":["choices can be a no-argument callable returning value and label pairs."]},{"id":"djangobeginner-005","courseId":"django-beginner","domain":"models-and-migrations","topic":"ForeignKey on_delete behavior","prompt":"A Car has a ForeignKey to Manufacturer with on_delete=models.PROTECT. Someone deletes a Manufacturer that still has cars. What happens?","hint":"Compare what each on_delete option does to the rows that point at the deleted object.","explain":"The on_delete option decides what happens to rows that point at a deleted object. Options differ: some remove those rows, some clear the link, and some refuse the delete.","source":"https://docs.djangoproject.com/en/stable/ref/models/fields/#django.db.models.ForeignKey.on_delete","interaction":{"type":"single"},"options":[{"id":"a","text":"The delete is refused with a ProtectedError, so the manufacturer remains.","why":"PROTECT prevents deletion of the referenced object by raising ProtectedError."},{"id":"b","text":"Every car is deleted along with the manufacturer.","whyNot":"That is CASCADE, which deletes the objects holding the ForeignKey; PROTECT does not."},{"id":"c","text":"Each car's maker field is set to NULL and the delete goes through.","whyNot":"That is SET_NULL, which needs null=True; PROTECT blocks the delete instead."},{"id":"d","text":"The manufacturer is deleted and the cars keep pointing at a missing row.","whyNot":"PROTECT does not allow this; it raises an error instead of leaving references behind."}],"answer":"a","explanation":"on_delete=PROTECT prevents deleting the referenced object by raising ProtectedError, instead of deleting or clearing related rows.","concept":"on_delete=PROTECT prevents deleting the referenced object by raising ProtectedError, instead of deleting or clearing related rows.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"class Car(models.Model):\n    maker = models.ForeignKey(\n        Manufacturer,\n        on_delete=models.PROTECT,\n    )","takeaways":["PROTECT blocks deleting a referenced row by raising ProtectedError."]}]