mirror of
https://github.com/HackSoftware/Django-Styleguide.git
synced 2025-04-23 18:11:59 +03:00
Add sub-section about Class-based vs. Function-based
This commit is contained in:
parent
d1df9895e3
commit
81d5e04f76
37
README.md
37
README.md
|
@ -37,6 +37,7 @@ Django styleguide that we use in [HackSoft](https://hacksoft.io).
|
|||
* [Testing](#testing-1)
|
||||
- [APIs & Serializers](#apis--serializers)
|
||||
* [Naming convention](#naming-convention-1)
|
||||
* [Class-based vs. Function-based](#class-based-vs-function-based)
|
||||
* [List APIs](#list-apis)
|
||||
+ [Plain](#plain)
|
||||
+ [Filters + Pagination](#filters--pagination)
|
||||
|
@ -690,6 +691,42 @@ For our APIs we use the following naming convention: `<Entity><Action>Api`.
|
|||
|
||||
Here are few examples: `UserCreateApi`, `UserSendResetPasswordApi`, `UserDeactivateApi`, etc.
|
||||
|
||||
### Class-based vs. Function-based
|
||||
|
||||
> This is mostly up to personal preferences, since you can achieve the same results with both approaches.
|
||||
|
||||
We have the following preferences:
|
||||
|
||||
1. Pick class-based APIS / views by default.
|
||||
1. If everyone else preferes & are comfortable with functions, use function-based APIs / views.
|
||||
|
||||
For us, the added benefits of using classes for APIs / views are the following:
|
||||
|
||||
1. You can inherit a `BaseApi` or add mixins.
|
||||
- If you are using function-based APIs / views, you'll need to do the same, but with decorators.
|
||||
2. The class creates a namespace where you can nest things (attributes, methods, etc.).
|
||||
- Additional API configuration can be done via class attributes.
|
||||
- In the case of function-based APIs / views, you need to stack decorators.
|
||||
|
||||
Here's an example with a class, inheriting a `BaseApi`:
|
||||
|
||||
```python
|
||||
class SomeApi(BaseApi):
|
||||
def get(self, request):
|
||||
data = something()
|
||||
|
||||
return Response(data)
|
||||
```
|
||||
|
||||
Here's an example with a function, using a `base_api` decorator (implementation is based on your needs)
|
||||
|
||||
```python
|
||||
@base_api(["GET"])
|
||||
def some_api(request):
|
||||
data = something()
|
||||
return Response(data)
|
||||
```
|
||||
|
||||
### List APIs
|
||||
|
||||
#### Plain
|
||||
|
|
Loading…
Reference in New Issue
Block a user