[{"id":"djangoadvanced-001","courseId":"django-advanced","domain":"models-and-migrations","topic":"Squashing migrations","prompt":"A production app must squash its migrations without breaking systems still on the old history. Put the steps of Django's recommended squashing process in order.","hint":"Old systems must still finish the old history before the old files can go.","explain":"Squashing reduces many migration files to a few, but running systems may still be partway through the old history. Consider which copies of the code must keep working during the change.","source":"https://docs.djangoproject.com/en/stable/topics/migrations/#squashing-migrations","interaction":{"type":"sort","items":[{"text":"Squash the migrations, keep the old files, and commit and release the result","why":"The squashed file ships beside the old ones, so systems partway through the history can still migrate."},{"text":"Wait until every instance runs the new release and has applied the squashed migration","why":"Old files can't go while some instances still depend on the old history."},{"text":"Delete the old migration files and commit that removal","why":"Once every instance has upgraded, the replaced files are no longer needed."},{"text":"Ship the removal as a second release","why":"The cleanup goes out separately, after the squash release has reached every system."}]},"options":[],"answer":"o:abcd","explanation":"Django's recommended squash keeps the old migration files until every instance has upgraded, then removes them in a second release.","concept":"Django's recommended squash keeps the old migration files until every instance has upgraded, then removes them in a second release.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Keep replaced migration files until every running instance has upgraded past them."]},{"id":"djangoadvanced-002","courseId":"django-advanced","domain":"models-and-migrations","topic":"Non-atomic migrations","prompt":"On MySQL, Django runs all operations of a migration inside a single transaction by default, so a failure part-way through rolls the whole migration back.","hint":"Consider which databases can roll back schema changes as one transaction.","explain":"Whether a migration is wrapped in one transaction depends on whether the database supports transactional schema changes. The atomic attribute on a Migration class controls this setting.","source":"https://docs.djangoproject.com/en/stable/topics/migrations/#transactions","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"The statement is wrong: MySQL doesn't support DDL transactions, so its operations run without one by default."},{"id":"b","text":"False","why":"On MySQL, migration operations run without a transaction by default, so there is no single transaction to roll back."}],"answer":"b","explanation":"On databases with DDL transactions (SQLite, PostgreSQL) a migration runs in one transaction by default; on databases like MySQL and Oracle it runs without one.","concept":"On databases with DDL transactions (SQLite, PostgreSQL) a migration runs in one transaction by default; on databases like MySQL and Oracle it runs without one.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Check whether your database supports transactional schema changes before assuming a failed migration is rolled back."]},{"id":"djangoadvanced-003","courseId":"django-advanced","domain":"models-and-migrations","topic":"Serializing values in migration files","prompt":"A field's default is a nested class, Outer.Choice, defined inside another class body. makemigrations must write that default into a migration file. What happens?","hint":"Think about where a class has to live for Django to refer to it by path.","explain":"Migration files are Python code, so Django has to turn each value into a valid expression. Some kinds of object have no standard way to be written out as code.","source":"https://docs.djangoproject.com/en/stable/topics/migrations/#serializing-values","interaction":{"type":"single"},"options":[{"id":"a","text":"Django can't serialize it: nested classes are unsupported, and class references must sit at module top level.","why":"Nested classes are on Django's list of things it cannot serialize, and class references must be in the module's top-level scope."},{"id":"b","text":"Django writes it as the dotted path Outer.Choice, which the migration import resolves.","whyNot":"Nested classes can't be serialized at all, so no dotted path to them is written."},{"id":"c","text":"Django serializes it only when Outer is a models.Model subclass.","whyNot":"Being inside a model doesn't help: nested classes are not serializable either way."},{"id":"d","text":"Django writes only the bare name Choice and drops Outer.","whyNot":"Django doesn't write a partial reference; it cannot serialize a nested class."}],"answer":"a","explanation":"Django can serialize a class reference only when the class is in the module's top-level scope; nested classes cannot be serialized.","concept":"Django can serialize a class reference only when the class is in the module's top-level scope; nested classes cannot be serialized.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Move classes used as field defaults or options to module top level."]},{"id":"djangoadvanced-004","courseId":"django-advanced","domain":"models-and-migrations","topic":"Unmanaged models","prompt":"A model sets managed = False to map a legacy table that another system owns. Which TWO statements about this model are true?","hint":"The managed option controls only one part of how Django treats a model.","explain":"The managed option decides whether Django changes the database for a model. Most other handling of the model, such as its fields and primary key, works as usual.","source":"https://docs.djangoproject.com/en/stable/ref/models/options/#managed","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"migrate creates, alters or drops no table for this model, because Django doesn't manage its table lifecycle.","why":"With managed=False, no table creation, modification or deletion operations are performed."},{"id":"b","text":"Django still adds an automatic primary key field if the model declares none.","why":"Adding an automatic primary key is one of the aspects that stay the same when managed=False."},{"id":"c","text":"flush empties and drops the table, the same as for any other model.","whyNot":"flush removes the tables of managed models; Django performs no deletion on an unmanaged model's table."},{"id":"d","text":"Test databases get the table automatically, so tests need no setup for it.","whyNot":"For unmanaged models, the docs leave it to you to make sure the right tables exist in test setup."},{"id":"e","text":"Its ManyToManyField intermediary table is created even when the other model is unmanaged too.","whyNot":"Between two unmanaged models the intermediary table is not created."},{"id":"f","text":"Its methods and Meta options stop applying, so it behaves like a plain mapping class.","whyNot":"Apart from table operations, all other model handling is exactly the same as normal."}],"answer":"ab","explanation":"managed=False stops Django from creating, altering or dropping the model's table, but everything else about the model is handled as normal.","concept":"managed=False stops Django from creating, altering or dropping the model's table, but everything else about the model is handled as normal.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Unmanaged models still get an automatic primary key, so declare every column of the existing table."]},{"id":"djangoadvanced-005","courseId":"django-advanced","domain":"models-and-migrations","topic":"SeparateDatabaseAndState operation","prompt":"A migration uses SeparateDatabaseAndState with both a database_operations list and a state_operations list. When migrate applies the database side, which list does Django run?","hint":"Separate the part that alters the schema from the part that updates Django's view of the models.","explain":"One migration operation can be split into two halves: one that changes the schema, and one that updates Django's internal picture of the models. The two halves can differ on purpose.","source":"https://docs.djangoproject.com/en/stable/ref/migration-operations/#separatedatabaseandstate","interaction":{"type":"single"},"options":[{"id":"a","text":"database_operations","why":"When asked to apply changes to the database, the operation uses the database_operations list."},{"id":"b","text":"state_operations","whyNot":"state_operations is used to apply state, which updates Django's model view, not the database."},{"id":"c","text":"Both lists, one after the other, against the database","whyNot":"Each list has its own job, so the operation does not run both against the database."},{"id":"d","text":"Neither; the lists are only read by makemigrations","whyNot":"makemigrations can check the state side, but the database side is the list that applies database changes."}],"answer":"a","explanation":"SeparateDatabaseAndState takes two lists: database_operations changes the schema, and state_operations updates only Django's model state.","concept":"SeparateDatabaseAndState takes two lists: database_operations changes the schema, and state_operations updates only Django's model state.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"migrations.SeparateDatabaseAndState(\n    database_operations=[...],\n    state_operations=[...],\n)","takeaways":["Keep the two halves in sync: a mismatch between them can break migrations or even lose data."]}]