Bird
0
0

How can you pass a Django model instance to a Celery task safely?

hard📝 Conceptual Q9 of 15
Django - Celery and Background Tasks
How can you pass a Django model instance to a Celery task safely?
AConvert the model instance to a string and pass it
BPass the entire model instance directly as a task argument
CSerialize the model instance with pickle and pass it
DPass the model's primary key and fetch the instance inside the task
Step-by-Step Solution
Solution:
  1. Step 1: Understand task argument serialization

    Celery serializes arguments; model instances are not safely serializable.
  2. Step 2: Identify safe practice

    Passing the primary key and querying inside the task avoids serialization issues.
  3. Final Answer:

    Pass the model's primary key and fetch the instance inside the task -> Option D
  4. Quick Check:

    Pass PK, not model instance, to tasks [OK]
Quick Trick: Pass model PK, fetch instance inside task [OK]
Common Mistakes:
MISTAKES
  • Passing model instance directly causes errors
  • Using pickle can cause security issues
  • Passing string representation loses data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes