📝 Use Tortoise's Tournement example for models
This commit is contained in:
parent
3165243755
commit
8f4648643d
1 changed files with 64 additions and 22 deletions
86
README.md
86
README.md
|
|
@ -55,14 +55,14 @@ In your browser go to:
|
||||||
Create a new app by:
|
Create a new app by:
|
||||||
|
|
||||||
```
|
```
|
||||||
ohmyapi startapp myapp
|
ohmyapi startapp tournament
|
||||||
```
|
```
|
||||||
|
|
||||||
This will create the following directory structure:
|
This will create the following directory structure:
|
||||||
|
|
||||||
```
|
```
|
||||||
myproject/
|
myproject/
|
||||||
- myapp/
|
- tournament/
|
||||||
- __init__.py
|
- __init__.py
|
||||||
- models.py
|
- models.py
|
||||||
- routes.py
|
- routes.py
|
||||||
|
|
@ -71,47 +71,69 @@ myproject/
|
||||||
- settings.py
|
- settings.py
|
||||||
```
|
```
|
||||||
|
|
||||||
Add 'myapp' to your `INSTALLED_APPS` in `settings.py`.
|
Add 'tournament' to your `INSTALLED_APPS` in `settings.py`.
|
||||||
|
|
||||||
### Models
|
### Models
|
||||||
|
|
||||||
Write your first model in `myapp/models.py`:
|
Write your first model in `turnament/models.py`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from ohmyapi.db import Model, field
|
from ohmyapi.db import Model, field
|
||||||
|
|
||||||
|
|
||||||
class Person(Model):
|
class Tournament(Model):
|
||||||
id: int = field.IntField(min=1, pk=True)
|
id = field.IntField(primary_key=True)
|
||||||
name: str = field.CharField(min_length=1, max_length=255)
|
name = field.TextField()
|
||||||
username: str = field.CharField(min_length=1, max_length=255, unique=True)
|
created = field.DatetimeField(auto_now_add=True)
|
||||||
age: int = field.IntField(min=0)
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
class Event(Model):
|
||||||
|
id = field.IntField(primary_key=True)
|
||||||
|
name = field.TextField()
|
||||||
|
tournament = field.ForeignKeyField('tournament.Tournament', related_name='events')
|
||||||
|
participants = field.ManyToManyField('torunament.Team', related_name='events', through='event_team')
|
||||||
|
modified = field.DatetimeField(auto_now=True)
|
||||||
|
prize = field.DecimalField(max_digits=10, decimal_places=2, null=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
class Team(Model):
|
||||||
|
id = field.IntField(primary_key=True)
|
||||||
|
name = field.TextField()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
```
|
```
|
||||||
|
|
||||||
### API Routes
|
### API Routes
|
||||||
|
|
||||||
Next, create your endpoints in `myapp/routes.py`:
|
Next, create your endpoints in `tournament/routes.py`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from ohmyapi.router import APIRouter, HTTPException
|
from ohmyapi.router import APIRouter, HTTPException
|
||||||
from ohmyapi.db.exceptions import DoesNotExist
|
from ohmyapi.db.exceptions import DoesNotExist
|
||||||
|
|
||||||
from .models import Person
|
from .models import Tournament
|
||||||
|
|
||||||
router = APIRouter(prefix="/myapp")
|
router = APIRouter(prefix="/tournament")
|
||||||
|
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def list():
|
async def list():
|
||||||
queryset = await Person.all()
|
queryset = Tournament.all()
|
||||||
return await Person.Schema.many.from_queryset(queryset)
|
return await Tournament.Schema.many.from_queryset(queryset)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/:id")
|
@router.get("/:id")
|
||||||
async def get(id: int):
|
async def get(id: int):
|
||||||
try:
|
try:
|
||||||
queryset = await Person.get(pk=id)
|
queryset = Tournament.get(pk=id)
|
||||||
return await Person.Schema.one(queryset)
|
return await Tournament.Schema.one(queryset)
|
||||||
except DoesNotExist:
|
except DoesNotExist:
|
||||||
raise HTTPException(status_code=404, detail="item not found")
|
raise HTTPException(status_code=404, detail="item not found")
|
||||||
|
|
||||||
|
|
@ -132,12 +154,12 @@ This will create a `migrations/` folder in you project root.
|
||||||
|
|
||||||
```
|
```
|
||||||
myproject/
|
myproject/
|
||||||
- myapp/
|
- tournament/
|
||||||
- __init__.py
|
- __init__.py
|
||||||
- models.py
|
- models.py
|
||||||
- routes.py
|
- routes.py
|
||||||
- migrations/
|
- migrations/
|
||||||
- myapp/
|
- tournament/
|
||||||
- pyproject.toml
|
- pyproject.toml
|
||||||
- README.md
|
- README.md
|
||||||
- settings.py
|
- settings.py
|
||||||
|
|
@ -184,6 +206,17 @@ JWT_SECRET = "t0ps3cr3t"
|
||||||
After restarting your project you will have access to the `ohmyapi_auth` app.
|
After restarting your project you will have access to the `ohmyapi_auth` app.
|
||||||
It comes with a `User` and `Group` model, as well as endpoints for JWT auth.
|
It comes with a `User` and `Group` model, as well as endpoints for JWT auth.
|
||||||
|
|
||||||
|
You can use the models as `ForeignKeyField` in your application models:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Team(Model):
|
||||||
|
[...]
|
||||||
|
members = field.ManyToManyField('ohmyapi_auth.User', related_name='tournament_teams', through='tournament_teams')
|
||||||
|
[...]
|
||||||
|
```
|
||||||
|
|
||||||
|
Remember to run `makemigrations` and `migrate` in order for your model changes to take effect in the database.
|
||||||
|
|
||||||
Create a super-user:
|
Create a super-user:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
@ -203,13 +236,22 @@ In your `routes.py`:
|
||||||
from ohmyapi.router import APIRouter, Depends
|
from ohmyapi.router import APIRouter, Depends
|
||||||
|
|
||||||
from ohmyapi_auth.models import User
|
from ohmyapi_auth.models import User
|
||||||
from ohmyapi_auth.permissions import require_authenticated
|
from ohmyapi_auth import (
|
||||||
|
models as auth,
|
||||||
|
permissions,
|
||||||
|
)
|
||||||
|
|
||||||
router = APIRouter(prefix="/myapp")
|
from .models import Tournament
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/tournament")
|
||||||
|
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
def must_be_authenticated(user: User = Depends(require_authenticated)):
|
async def list(user: auth.User = Depends(permissions.require_authenticated)):
|
||||||
return {"user": user}
|
queryset = Tournament.all()
|
||||||
|
return await Tournament.Schema.many.from_queryset(queryset)
|
||||||
|
|
||||||
|
|
||||||
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue