2025.07.23.(Wed)

1. What is the difference between null=True and blank=True in Django models?

  • null=True: The database column can store NULL (i.e., no value). Used for database-level validation.

  • blank=True: The field is allowed to be empty in forms. Used for form-level validation.

ex)


name = models.CharField(max_length=100, null=True, blank=True)

2. Explain the lifecycle of a Django request.

  1. Request comes to WSGI server (like Gunicorn).

  2. Routed through Django’s middleware stack.

  3. urls.py matches the URL to a view.

  4. View executes logic and queries models if needed.

  5. Template renders response (if HTML).

  6. Middleware processes response.

  7. Final response returned to the client.

3. How does Django’s ORM prevent SQL injection?

Django ORM uses parameterized queries internally. When you use queryset methods (.filter(), .get(), etc.), Django escapes inputs and avoids raw SQL, protecting against SQL injection.

4. What are Django signals and when would you use them?

Django signals allow decoupled apps to get notified when certain actions occur (e.g., post-save, pre-delete).


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

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
  if created:
    Profile.objects.create(user=instance)

Decoupled apps (디커플드 앱) 란?

‘Decoupled’는 ‘느슨하게 연결된’, ‘독립적인’이란 뜻이다. 즉, 디커플드 앱은 서로 강하게 의존하지 않고 독립적으로 동작하는 앱을 말한다.

  • 하나의 앱이 다른 앱에 너무 의존하지 않고
  • 자신의 역할만 충실히 하면서
  • 필요할 때만 신호(이벤트)를 통해서 다른 앱과 소통하는 구조다.

왜 중요한가?

  • 앱끼리 너무 밀접하게 연결되어 있으면, 한쪽을 고치고 바꾸기 어려워진다.
  • 디커플드 구조는 유지보수와 확장성이 좋아서, 큰 프로젝트에서 관리하기 편해진다.

예를 들어:

  • User 앱은 사용자 계정을 관리하고,

  • Profile 앱은 사용자 프로필을 관리한다.

User가 생성될 때 Profile 을 자동으로 만드는 기능은 User 앱과 Profile 앱이 강하게 묶이지 않고 신호를 통해 느슨하게 연결되어 구현된다.

5. How can you improve the performance of a Django application?

  • user select_related and prefetch_related to optimize DB queries.

  • Implement caching (e.g., Redis, Memcached).

  • Enable database indexing on frequently queried fields.

  • Minimize N+1 queries

  • Use Django Debug Toolbar during development.

  • Serve static/media fiels via CDN in production.