#NoStupidQuestion: On a Django app, if building a new view for a frontend where should you get the objects? from the models or Django admin?

Photo by Faisal on Unsplash

#NoStupidQuestion: On a Django app, if building a new view for a frontend where should you get the objects? from the models or Django admin?

·

2 min read

When building a new view for a frontend, you should get the objects from the models, not the Django admin. Here's why:


1. Separation of Concerns

  • Django Admin is intended for backend administration and internal use. It provides a user interface for managing data but is not designed to serve as the backend logic for frontend views.

  • Django Models are the foundation of your application's data layer. They provide direct access to the database and are designed to be used for both backend and frontend views.


2. Flexibility and Scalability

  • Fetching data from models using Django's ORM (e.g., Model.objects.filter(...)) gives you complete control over queries, allowing you to tailor the data specifically for your view.

  • Using models avoids tight coupling with admin-specific configurations (like ModelAdmin customizations), making your views reusable and easier to maintain.


3. Code Maintainability

  • Frontend views should rely on models because this keeps your business logic centralized. Admin configurations, such as list_display or list_filter, are specific to the admin interface and are not reusable in frontend code.

4. Access and Permissions

  • In frontend views, you can implement custom access control and permissions directly using Django's tools like user.has_perm().

  • Django Admin's permissions and configurations are backend-focused, and using them directly for a frontend could lead to security risks or unnecessary complexity.


How to Use Models in Frontend Views

Example: Fetching Data from a Model for a Frontend View

Here’s how you would fetch objects directly from the models in views.py:

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
from .models import ProductConsumption

@login_required
def example_view(request):
    if request.user.has_perm('app.view_examplemodels'):
        # Fetch data from the model
        data = ExampleModel.objects.all()

        # Pass the data to the template
        return render(request, 'interface/base.html', {
            'data': data
        })
    else:
        return HttpResponseForbidden("You don't have permission to view this page.")

Why Not Use Admin for Frontend?

  • Tight Coupling: Admin customizations (ModelAdmin) are designed for internal data management, not for the logic required by frontend views.

  • Unoptimized Queries: Admin often uses its own ORM configurations (like list_filter) that might not match the specific needs of your frontend.

  • Security Concerns: Exposing admin configurations to the frontend can create potential vulnerabilities, as admin is meant to be an internal tool.


When to Use Django Admin

Django Admin is best used for internal dashboards or when developing management interfaces that are accessible only to staff. For user-facing features, always rely on models and tailor the views using Django's tools (views.py, serializers, etc.).

Did you find this article valuable?

Support anj in tech by becoming a sponsor. Any amount is appreciated!