📝 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:
|
||||
|
||||
```
|
||||
ohmyapi startapp myapp
|
||||
ohmyapi startapp tournament
|
||||
```
|
||||
|
||||
This will create the following directory structure:
|
||||
|
||||
```
|
||||
myproject/
|
||||
- myapp/
|
||||
- tournament/
|
||||
- __init__.py
|
||||
- models.py
|
||||
- routes.py
|
||||
|
|
@ -71,47 +71,69 @@ myproject/
|
|||
- settings.py
|
||||
```
|
||||
|
||||
Add 'myapp' to your `INSTALLED_APPS` in `settings.py`.
|
||||
Add 'tournament' to your `INSTALLED_APPS` in `settings.py`.
|
||||
|
||||
### Models
|
||||
|
||||
Write your first model in `myapp/models.py`:
|
||||
Write your first model in `turnament/models.py`:
|
||||
|
||||
```python
|
||||
from ohmyapi.db import Model, field
|
||||
|
||||
|
||||
class Person(Model):
|
||||
id: int = field.IntField(min=1, pk=True)
|
||||
name: str = field.CharField(min_length=1, max_length=255)
|
||||
username: str = field.CharField(min_length=1, max_length=255, unique=True)
|
||||
age: int = field.IntField(min=0)
|
||||
class Tournament(Model):
|
||||
id = field.IntField(primary_key=True)
|
||||
name = field.TextField()
|
||||
created = field.DatetimeField(auto_now_add=True)
|
||||
|
||||
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
|
||||
|
||||
Next, create your endpoints in `myapp/routes.py`:
|
||||
Next, create your endpoints in `tournament/routes.py`:
|
||||
|
||||
```python
|
||||
from ohmyapi.router import APIRouter, HTTPException
|
||||
from ohmyapi.db.exceptions import DoesNotExist
|
||||
|
||||
from .models import Person
|
||||
from .models import Tournament
|
||||
|
||||
router = APIRouter(prefix="/myapp")
|
||||
router = APIRouter(prefix="/tournament")
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def list():
|
||||
queryset = await Person.all()
|
||||
return await Person.Schema.many.from_queryset(queryset)
|
||||
queryset = Tournament.all()
|
||||
return await Tournament.Schema.many.from_queryset(queryset)
|
||||
|
||||
|
||||
@router.get("/:id")
|
||||
async def get(id: int):
|
||||
try:
|
||||
queryset = await Person.get(pk=id)
|
||||
return await Person.Schema.one(queryset)
|
||||
queryset = Tournament.get(pk=id)
|
||||
return await Tournament.Schema.one(queryset)
|
||||
except DoesNotExist:
|
||||
raise HTTPException(status_code=404, detail="item not found")
|
||||
|
||||
|
|
@ -132,12 +154,12 @@ This will create a `migrations/` folder in you project root.
|
|||
|
||||
```
|
||||
myproject/
|
||||
- myapp/
|
||||
- tournament/
|
||||
- __init__.py
|
||||
- models.py
|
||||
- routes.py
|
||||
- migrations/
|
||||
- myapp/
|
||||
- tournament/
|
||||
- pyproject.toml
|
||||
- README.md
|
||||
- settings.py
|
||||
|
|
@ -184,6 +206,17 @@ JWT_SECRET = "t0ps3cr3t"
|
|||
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.
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
|
|
@ -203,13 +236,22 @@ In your `routes.py`:
|
|||
from ohmyapi.router import APIRouter, Depends
|
||||
|
||||
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("/")
|
||||
def must_be_authenticated(user: User = Depends(require_authenticated)):
|
||||
return {"user": user}
|
||||
async def list(user: auth.User = Depends(permissions.require_authenticated)):
|
||||
queryset = Tournament.all()
|
||||
return await Tournament.Schema.many.from_queryset(queryset)
|
||||
|
||||
|
||||
...
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue