-
Notifications
You must be signed in to change notification settings - Fork 4
Naming Convention
Amin Zamani edited this page Nov 20, 2023
·
1 revision
When using FastAPI, following a consistent and clear naming convention for your models, schemas, and services can enhance code readability and maintainability. Here are some best practices:
-
Use Singular Nouns:
- Name your models with singular nouns to represent a single instance of the entity.
class User(BaseModel): username: str email: str
-
Be Descriptive:
- Choose descriptive names that convey the purpose of the model.
class Item(BaseModel): name: str description: str
-
Use CamelCase:
- CamelCase is a common convention for naming schemas.
class ItemResponse(BaseModel): name: str description: str
-
Include the Word "Response" or "Request" for Clarity:
- Make it clear whether a schema is used for request or response purposes.
class ItemCreateRequest(BaseModel): name: str description: str
-
Use Singular Nouns for Service Classes:
- Similar to models, use singular nouns for service classes.
class UserService: def create_user(self, user: User): # implementation
-
Be Explicit:
- Choose names that clearly indicate the service's functionality.
class EmailService: def send_email(self, to: str, subject: str, body: str): # implementation
-
Follow CRUD Naming Conventions:
- For services that perform CRUD operations, follow naming conventions like
get
,create
,update
, anddelete
.
class ItemService: def create_item(self, item: Item): # implementation def get_item(self, item_id: int): # implementation def update_item(self, item_id: int, item: Item): # implementation def delete_item(self, item_id: int): # implementation
- For services that perform CRUD operations, follow naming conventions like
-
Use Consistent Prefixes:
- If needed, use consistent prefixes for related classes to group them visually.
class UserModel: # ... class UserSchema: # ... class UserService: # ...
-
Avoid Ambiguous Names:
- Ensure that your names are not ambiguous and clearly convey the purpose of each class.
Remember that these are general guidelines, and the most important thing is to be consistent within your project. Choose a convention that fits your team's style and stick to it throughout your codebase.
- Introduction
- Variables
- Data Types
- Numbers
- Casting
- Strings
- Booleans
- Operators
- Lists
- Tuple
- Sets
- Dictionaries
- Conditionals
- Loops
- Functions
- Lambda
- Classes
- Inheritance
- Iterators
- Multi‐Processing
- Multi‐Threading
- I/O Operations
- How can I check all the installed Python versions on Windows?
- Hello, world!
- Python literals
- Arithmetic operators and the hierarchy of priorities
- Variables
- Comments
- The input() function and string operators
Boolean values, conditional execution, loops, lists and list processing, logical and bitwise operations
- Comparison operators and conditional execution
- Loops
- [Logic and bit operations in Python]
- [Lists]
- [Sorting simple lists]
- [List processing]
- [Multidimensional arrays]
- Introduction
- Sorting Algorithms
- Search Algorithms
- Pattern-matching Algorithm
- Graph Algorithms
- Machine Learning Algorithms
- Encryption Algorithms
- Compression Algorithms
- Start a New Django Project
- Migration
- Start Server
- Requirements
- Other Commands
- Project Config
- Create Data Model
- Admin Panel
- Routing
- Views (Function Based)
- Views (Class Based)
- Django Template
- Model Managers and Querysets
- Form
- User model
- Authentification
- Send Email
- Flash messages
- Seed
- Organize Logic
- Django's Business Logic Services and Managers
- TestCase
- ASGI and WSGI
- Celery Framework
- Redis and Django
- Django Local Network Access
- Introduction
- API development
- API architecture
- lifecycle of APIs
- API Designing
- Implementing APIs
- Defining the API specification
- API Testing Tools
- API documentation
- API version
- REST APIs
- REST API URI naming rules
- Automated vs. Manual Testing
- Unit Tests vs. Integration Tests
- Choosing a Test Runner
- Writing Your First Test
- Executing Your First Test
- Testing for Django
- More Advanced Testing Scenarios
- Automating the Execution of Your Tests
- End-to-end
- Scenario
- Python Syntax
- Python OOP
- Python Developer position
- Python backend developer
- Clean Code
- Data Structures
- Algorithms
- Database
- PostgreSQL
- Redis
- Celery
- RabbitMQ
- Unit testing
- Web API
- REST API
- API documentation
- Django
- Django Advance
- Django ORM
- Django Models
- Django Views
- Django Rest Framework
- Django Rest Framework serializers
- Django Rest Framework views
- Django Rest Framework viewsets