ExamHoot

Django Model Signals and Database Consistency

from django.db.models.signals import post_save
from django.dispatch import receiver

class Product(models.Model):
    name = models.CharField(max_length=100)
    stock = models.IntegerField(default=0)

class Order(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.IntegerField()

@receiver(post_save, sender=Order)
def update_stock(sender, instance, created, **kwargs):
    if created:
        product = instance.product
        product.stock -= instance.quantity
        product.save()

# Hari creates an order:
order = Order.objects.create(product=product, quantity=5)
  1. This approach is reliable for maintaining stock consistency because signals are always executed before the transaction commits.

  2. This approach has potential issues: if the signal fails, the order is saved but stock isn't updated. Using database constraints or F() expressions would be safer: Product.objects.filter(pk=product.pk).update(stock=F('stock')-quantity).

  3. Signals cannot be used for database operations and will raise an error.

  4. The code is correct but requires wrapping in a transaction.atomic() block to work.

Show answer & explanation

Correct answer

This approach has potential issues: if the signal fails, the order is saved but stock isn't updated. Using database constraints or F() expressions would be safer: Product.objects.filter(pk=product.pk).update(stock=F('stock')-quantity).

Explanation

Signals are executed in the same database transaction, but they can cause issues if the signal handler fails or if there are race conditions. Using atomic transactions and database-level constraints (like triggers or check constraints) is more reliable for maintaining data consistency.

Written by ExamHoot EditorialPublished · Updated

All 30 Django Models and Database Design questions

  1. 1.What is the primary purpose of Django Models?
  2. 2.Which field type should be used to store an email address in a Django Model?
  3. 3.What does the Meta class inside a Django Model do?
  4. 4.Which relationship type is used when one model can have many instances of another model?
  5. 5.What is the purpose of the `__str__` method in a Django Model?
  6. 6.Which field attribute makes a field mandatory in Django Models?
  7. 7.What does the `on_delete` parameter do in a ForeignKey relationship?
  8. 8.Which of the following is a valid Django field type for storing boolean values?
  9. 9.What is the default behavior when you don't specify `null=True` for a field in Django?
  10. 10.Which method is used to retrieve a single object from the database in Django?
  11. 11.Understanding Django Model Meta Options
  12. 12.Scenario:
  13. 13.Django QuerySet Filter vs Get Method
  14. 14.Coding:
  15. 15.Scenario:
  16. 16.Understanding Django Model Inheritance
  17. 17.Coding:
  18. 18.Scenario:
  19. 19.Django Model Manager and QuerySet Methods
  20. 20.Coding:
  21. 21.Django Model Inheritance Strategy Selection
  22. 22.Django QuerySet Optimization with select_related vs prefetch_related
  23. 23.Django Model Field Choices and Validation
  24. 24.Django Model Relationships:
  25. 25.Django Model Signals and Database Consistency
  26. 26.Django Model Meta Options:
  27. 27.Django Model Managers and Custom QuerySets
  28. 28.Django Model Field:
  29. 29.Django Model Validation:
  30. 30.Django Model Aggregation and Annotation