Consider this Django view using a mixin to add a greeting message:
from django.views import View
class GreetingMixin:
def get_greeting(self):
return "Hello from mixin!"
class MyView(GreetingMixin, View):
def get(self, request):
return self.get_greeting()What will MyView().get(request) return?
from django.views import View class GreetingMixin: def get_greeting(self): return "Hello from mixin!" class MyView(GreetingMixin, View): def get(self, request): return self.get_greeting()
Check which class provides the get_greeting method and how inheritance works.
The GreetingMixin defines get_greeting. Since MyView inherits from it, calling self.get_greeting() returns the string from the mixin.
You want to create a mixin that adds a created_at timestamp field to any Django model. Which option is syntactically correct and follows Django model mixin patterns?
Remember Django mixins for models should be abstract base classes to avoid creating separate tables.
Option A correctly defines an abstract model mixin with auto_now_add=True for creation timestamp. Option A lacks model inheritance. Option A is not abstract, so it creates a table. Option A uses auto_now=True which updates timestamp on every save, not just creation.
Given these classes:
class LogMixin:
def dispatch(self, request, *args, **kwargs):
print("Logging request")
return super().dispatch(request, *args, **kwargs)
class MyView(LogMixin, View):
def dispatch(self, request, *args, **kwargs):
print("MyView dispatch")
return super().dispatch(request, *args, **kwargs)When calling MyView().dispatch(request), a RuntimeError: maximum recursion depth exceeded occurs. Why?
class LogMixin: def dispatch(self, request, *args, **kwargs): print("Logging request") return super().dispatch(request, *args, **kwargs) class MyView(LogMixin, View): def dispatch(self, request, *args, **kwargs): print("MyView dispatch") return super().dispatch(request, *args, **kwargs)
Think about the method resolution order and what happens when super() calls a method that calls super() again.
Both LogMixin and MyView override dispatch and call super().dispatch(). If the base View class does not implement dispatch, super() calls circle back causing infinite recursion.
self.counter after calling this mixin method twice?Given this mixin and class:
class CounterMixin:
def __init__(self):
self.counter = 0
def increment(self):
self.counter += 1
class MyClass(CounterMixin):
def __init__(self):
super().__init__()What is the value of self.counter after this code?
obj = MyClass() obj.increment() obj.increment()
class CounterMixin: def __init__(self): self.counter = 0 def increment(self): self.counter += 1 class MyClass(CounterMixin): def __init__(self): super().__init__() obj = MyClass() obj.increment() obj.increment()
Check if the mixin's __init__ is called and how increment changes counter.
The mixin initializes counter to 0. Each call to increment adds 1. After two calls, counter is 2.
Choose the most accurate description of mixins in Django development.
Think about how mixins help share code in classes without affecting database schema.
Mixins are classes designed to add reusable methods or properties to other classes via multiple inheritance, commonly used in Django for views and models without creating new tables.