Django

Django Signals

Built-in signals, pre_save, post_save, pre_delete, custom signals, receivers, signal best practices

18 pertanyaan wawancara·
Mid-Level
1

What is a signal in Django?

Jawaban

A signal is a mechanism allowing certain parts of the application to be notified when specific actions occur elsewhere in the framework. Signals enable decoupling between the event sender and the receivers that react to that event. Django provides several built-in signals like pre_save, post_save, pre_delete and post_delete for model operations.

2

Which signal is emitted just before an object is saved to the database?

Jawaban

The pre_save signal is emitted before a model's save() method is executed. It allows making modifications to the instance before it is saved, such as calculating derived values or normalizing data. This signal is useful because it is called whether the object is being created or updated.

3

Which decorator should be used to connect a receiver function to a Django signal?

Jawaban

The @receiver decorator from django.dispatch allows connecting a function to a signal declaratively. It takes the signal to listen to as a parameter and optionally the sender to filter emitters. This approach is more readable than the connect() method and makes code maintenance easier.

4

Which argument of the post_save signal allows distinguishing a creation from an update?

5

Where is it recommended to place signal receiver code in a Django application?

+15 pertanyaan wawancara

Kuasai Django untuk wawancara berikutnya

Akses semua pertanyaan, flashcards, tes teknis, latihan code review dan simulator wawancara.

Mulai gratis