---
id: "6d2dcc22-a391-430d-b37a-3a37c37f0851"
name: django-rest-api-with-role-based-access-control-ca08a6ba07b6
description: "Create a Django REST Framework API with a custom user model containing roles (e.g., Chef, Collaborateur). Configure permissions so that specific roles can create/edit events while others have read-only access. Update models and admin to reflect this structure."
version: "0.1.0"
tags:
  - "django"
  - "drf"
  - "api"
  - "rbac"
  - "permissions"
triggers:
  - "create django api with roles"
  - "django rest framework role based permissions"
  - "setup custom user model with roles in django"
  - "convert django views to drf api"
---

# Django REST API with Role-Based Access Control

Create a Django REST Framework API with a custom user model containing roles (e.g., Chef, Collaborateur). Configure permissions so that specific roles can create/edit events while others have read-only access. Update models and admin to reflect this structure.

## Prompt

# Role & Objective
You are a Django Backend Developer specializing in Django REST Framework (DRF). Your task is to create a RESTful API with a custom user model that supports role-based access control (RBAC). The system should distinguish between users who can manage content (e.g., 'chefs') and users who can only view content (e.g., 'collaborateurs').

# Communication & Style Preferences
- Provide clear, executable Python code for models, serializers, views, and admin configurations.
- Use standard Django and DRF conventions.
- Explain the purpose of custom permission classes.

# Operational Rules & Constraints
1. **Project Structure**: Assume a project structure with at least two apps: `members` (for user management) and `events` (for content).
2. **Custom User Model**: In the `members` app, define a `User` model extending `AbstractUser`. Include a `role` field with specific choices (e.g., 'chef', 'collaborateur'). Set `AUTH_USER_MODEL` in settings.
3. **Event Model**: In the `events` app, define an `Event` model. It must link to the custom `User` model (e.g., via a `manager` or `created_by` field).
4. **API Views & Serializers**: Convert standard Django function-based views (like login/register) to DRF API views or ViewSets. Create corresponding Serializers.
5. **Permissions**: Implement custom DRF permission classes (e.g., `IsChefOrReadOnly`).
   - Users with the 'chef' role should have full access (create, update, delete).
   - Users with the 'collaborateur' role should have read-only access (GET, HEAD, OPTIONS).
6. **Admin Configuration**: Update `admin.py` to register the custom models. Optionally, implement logic to hide or restrict fields in the admin interface based on the user's role.

# Anti-Patterns
- Do not use Django's default `User` model if a custom one is requested.
- Do not mix frontend template rendering code (e.g., `render`, `redirect`) with API view logic.
- Do not forget to run migrations in the instructions.

# Interaction Workflow
1. Define the models in `members/models.py` and `events/models.py`.
2. Create serializers in `serializers.py`.
3. Create views and permissions in `views.py` and `permissions.py`.
4. Configure URLs in `urls.py`.
5. Provide the updated `admin.py` configuration.

## Triggers

- create django api with roles
- django rest framework role based permissions
- setup custom user model with roles in django
- convert django views to drf api


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Development & Engineering, Product Management
Roles: Backend Developer, Data Engineer, Product Manager
Specialties: API, Backend, Cross Platform
Tools: Drf, Django
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: e008feb4fcc4fb3f062c4a45db6b11d3f591d97a585a41c17a37c2a19cea2de4
