Models & ORM Questions with Answers

34 practice questions on Models & ORM (Django) with answers and step-by-step solutions. Try 10 samples below, then practise all 34 free in the AptiNest app.

Sample questions

Question 1Easy

Which field type stores short text with a required max_length?

  1. TextField
  2. StringField
  3. VarcharField
  4. CharField

Answer: D. CharField

Explanation

CharField maps to VARCHAR; TextField is for long text.

Step-by-step solution

  1. Set max_length.
  2. Use CharField.

Why the other options are wrong

  • A. TextField has no max_length requirement.
  • B. StringField is not a Django model field; the correct choice is CharField.
  • C. VarcharField is not a Django model field; the correct choice is CharField.

Question 2Easy

Which is the correct way to define a many-to-many relation between Student and Course?

  1. courses = models.ManyField(Course)
  2. courses = models.ManyToManyField(Course)
  3. courses = models.ForeignKey(Course, many=True)
  4. courses = models.ListField(Course)

Answer: B. courses = models.ManyToManyField(Course)

Explanation

Django creates an automatic join table; use through for extra columns.

Step-by-step solution

  1. Add the field on one side.
  2. Join table is made.

Why the other options are wrong

  • A. courses = models.ManyField(Course) is not a Django model field; the correct choice is courses = models.ManyToManyField(Course).
  • C. ForeignKey has no many argument.
  • D. courses = models.ListField(Course) is not a Django model field; the correct choice is courses = models.ManyToManyField(Course).

Question 3Easy

Which call creates and saves an object in one step?

  1. Book.save(title='A')
  2. Book.objects.insert(title='A')
  3. Book.objects.create(title='A')
  4. Book.new(title='A').commit()

Answer: C. Book.objects.create(title='A')

Explanation

The manager's create() builds and saves the instance.

Step-by-step solution

  1. Book.objects.create() creates and saves the object in one step.
  2. Other methods require separate steps for creation and saving.
  3. The correct answer is Book.objects.create(title='A').

Why the other options are wrong

  • A. Calling save on the class with a title does not create the row; save() needs an instance.
  • B. Book.objects.insert is not a Django manager method; create() is the method that creates and saves.
  • D. Book.new(...).commit() is not Django API; instances are saved with save(), not commit().

Tip: Use create() for instant object creation and saving.

Question 4Easy

Which base class must a Django model inherit from?

  1. django.db.Table
  2. django.models.Base
  3. django.db.models.Entity
  4. django.db.models.Model

Answer: D. django.db.models.Model

Explanation

Every model subclasses models.Model.

Step-by-step solution

  1. Django models are classes that inherit from a base class.
  2. The base class is called django.db.models.Model.
  3. All models must subclass this to work with the ORM.
  4. This ensures access to database operations.
  5. Final answer: django.db.models.Model.

Why the other options are wrong

  • A. django.db.Table is not a base class for models; Django models subclass django.db.models.Model.
  • B. django.models.Base does not exist; the model base class is in django.db.models.
  • C. django.db.models.Entity is not a Django class; the base is django.db.models.Model.

Tip: Remember, every Django model inherits from Model.

Question 5Medium

What happens to Model.objects.filter() evaluated twice?

  1. It is always cached globally by Django, so a second query never reaches the DB
  2. Each new queryset hits the database separately; reusing the same evaluated one uses its cache
  3. It raises an error
  4. It reads from the session

Answer: B. Each new queryset hits the database separately; reusing the same evaluated one uses its cache

Explanation

A QuerySet caches its results after the first evaluation.

Step-by-step solution

  1. Same object caches.
  2. New queryset queries again.

Why the other options are wrong

  • A. No global cache.
  • C. No error.
  • D. Query results are cached on the QuerySet object, not read back from the session.

Question 6Medium

What is the difference between get() and filter() on a QuerySet?

  1. get() returns a QuerySet and filter() returns exactly one object
  2. get() returns one object or raises; filter() returns a QuerySet
  3. They are identical because get() is simply an alias of filter()
  4. filter() raises DoesNotExist whenever no row matches the condition

Answer: B. get() returns one object or raises; filter() returns a QuerySet

Explanation

get raises DoesNotExist or MultipleObjectsReturned; filter returns a possibly empty QuerySet.

Step-by-step solution

  1. get() returns exactly one object.
  2. If no row matches it raises DoesNotExist; if several match it raises MultipleObjectsReturned.
  3. filter() returns a QuerySet, which can be empty or hold many rows.
  4. So the answer is option B.

Why the other options are wrong

  • A. This swaps the two: get() returns one object and filter() returns a QuerySet.
  • C. get() is not an alias of filter(); they differ in return type and errors.
  • D. filter() does not raise DoesNotExist; it returns an empty QuerySet.

Tip: Use get() when you expect exactly one row; use filter() for any number of rows.

Question 7Medium

Which object lets you build OR conditions in filter()?

  1. F objects, e.g. F(a) | F(b) in a filter call
  2. Case objects, combined with the plus operator
  3. Prefetch objects passed as arguments to filter
  4. Q objects, e.g. Q(a=1) | Q(b=2)

Answer: D. Q objects, e.g. Q(a=1) | Q(b=2)

Explanation

Q objects combine conditions with | and &; F references fields.

Step-by-step solution

  1. filter() takes keyword conditions, and Q objects let you combine them.
  2. Q objects use | for OR and & for AND.
  3. Q(a=1) | Q(b=2) matches rows where a is 1 or b is 2.
  4. So the answer is option D, Q objects.

Why the other options are wrong

  • A. F objects refer to a field's value; they do not build OR conditions.
  • B. Case objects are conditional expressions, not OR conditions in filter().
  • C. Prefetch objects control related loading, not filter conditions.

Tip: Q(...) | Q(...) is OR and Q(...) & Q(...) is AND.

Question 8Medium

Which lookup performs a case-insensitive substring match?

  1. name__icontains='abc'
  2. name__contains_i='abc'
  3. name__like='abc'
  4. name__iexact='abc'

Answer: A. name__icontains='abc'

Explanation

icontains is case-insensitive containment; iexact is a full case-insensitive match.

Step-by-step solution

  1. The lookup must match a substring and ignore case.
  2. The i prefix makes a lookup case-insensitive, and contains means substring.
  3. name__icontains='abc' matches values such as 'ABC' and 'xabcx'.
  4. So the answer is A.

Why the other options are wrong

  • B. contains_i is not a Django lookup name.
  • C. like is not a Django lookup; the lookups are contains and icontains.
  • D. iexact needs the whole value to match, not a substring.

Tip: Add i to a lookup for case-insensitive: iexact, icontains, istartswith.

Question 9Hard

Which statement about bulk_create() is correct?

  1. It skips each object's save() and the save signals
  2. It calls save() on every object and sends the signals
  3. It also updates rows that already exist in the table
  4. It always returns objects with related data prefetched

Answer: A. It skips each object's save() and the save signals

Explanation

bulk_create bypasses save() and signals.

Step-by-step solution

  1. bulk_create() inserts many objects in one query.
  2. It does not call each object's save() method.
  3. So the save signals are not sent for those objects.
  4. So the answer is option A.

Why the other options are wrong

  • B. bulk_create() skips save() and signals; this is why it is fast.
  • C. bulk_create() inserts new rows; it does not update existing ones.
  • D. It returns the created objects but does not prefetch related data.

Tip: Use bulk_create() for speed; use save() in a loop if save logic or signals matter.

Question 10Hard

What does related_name='posts' on a ForeignKey change?

  1. The column name used in the database table that stores the foreign key value
  2. The model's table name
  3. The reverse accessor name, e.g. author.posts instead of author.post_set
  4. The delete behaviour

Answer: C. The reverse accessor name, e.g. author.posts instead of author.post_set

Explanation

It also is the reverse query name used in filters unless related_query_name is set.

Step-by-step solution

  1. FK defined on Post.
  2. Reverse access from the author.

Why the other options are wrong

  • A. db_column does that.
  • B. db_table does that.
  • D. on_delete does that.

Practise all 34 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Django topics · All topics