# FastAPI Admin

***

**1. Create a basic FastAPI-Admin app**

```python
from fastapi import FastAPI
from fastapi_admin.app import FastAPIAdmin

app = FastAPI()
admin = FastAPIAdmin(app)
```

**2. Add a model to the admin interface**

```python
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str

admin.add_model(User)
```

**3. Customize the model's display fields**

```python
class User(BaseModel):
    id: int
    name: str

admin.add_model(User, display_fields=("id", "name"))
```

**4. Add a custom action to the model**

```python
@admin.add_action("delete")
def delete_user(pk):
    # Delete the user with the given primary key
    pass
```

**5. Customize the action's display name**

```python
@admin.add_action("delete", display_name="Delete User")
def delete_user(pk):
    # Delete the user with the given primary key
    pass
```

**6. Add a custom filter to the model**

```python
@admin.add_filter("active")
def is_active(query):
    # Filter the query to only include active users
    return query.filter(User.is_active == True)
```

**7. Customize the filter's display name**

```python
@admin.add_filter("active", display_name="Active")
def is_active(query):
    # Filter the query to only include active users
    return query.filter(User.is_active == True)
```

**8. Add a custom export format to the model**

```python
@admin.add_export_format("csv")
def export_to_csv(queryset):
    # Export the queryset to a CSV file
    pass
```

**9. Customize the export format's display name**

```python
@admin.add_export_format("csv", display_name="CSV")
def export_to_csv(queryset):
    # Export the queryset to a CSV file
    pass
```

**10. Add a custom import handler to the model**

```python
@admin.add_import_handler("csv")
def import_from_csv(file):
    # Import data from a CSV file
    pass
```

**11. Customize the import handler's display name**

```python
@admin.add_import_handler("csv", display_name="Import from CSV")
def import_from_csv(file):
    # Import data from a CSV file
    pass
```

**12. Add a custom view to the model**

```python
@admin.add_view("detail")
def user_detail(request, pk):
    # Get the user with the given primary key
    user = User.get(pk)
    return templates.TemplateResponse("user_detail.html", {"user": user})
```

**13. Customize the view's display name**

```python
@admin.add_view("detail", display_name="Detail")
def user_detail(request, pk):
    # Get the user with the given primary key
    user = User.get(pk)
    return templates.TemplateResponse("user_detail.html", {"user": user})
```

**14. Add a custom link to the navigation bar**

```python
@admin.add_link("documentation")
def documentation_link(request):
    return {"url": "https://fastapi-admin.readthedocs.io/en/latest/", "name": "Documentation"}
```

**15. Customize the link's display name**

```python
@admin.add_link("documentation", display_name="Documentation")
def documentation_link(request):
    return {"url": "https://fastapi-admin.readthedocs.io/en/latest/", "name": "Documentation"}
```

**16. Add a custom menu item to the sidebar**

```python
@admin.add_menu_item("settings")
def settings_menu_item(request):
    return {"url": "/settings/", "name": "Settings"}
```

**17. Customize the menu item's display name**

```python
@admin.add_menu_item("settings", display_name="Settings")
def settings_menu_item(request):
    return {"url": "/settings/", "name": "Settings"}
```

**18. Add a custom theme to the admin interface**

```python
from fastapi_admin.themes import SimpleTheme

admin.theme = SimpleTheme()
```

**19. Customize the theme's colors**

```python
from fastapi_admin.themes import SimpleTheme

theme = SimpleTheme()
theme.primary_color = "#007bff"
theme.accent_color = "#6c757d"

admin.theme = theme
```

**20. Customize the theme's fonts**

```python
from fastapi_admin.themes import SimpleTheme

theme = SimpleTheme()
theme.font_family = "Arial"
theme.font_size = "14px"

admin.theme = theme
```

**21. Customize the theme's layout**

```python
from fastapi_admin.themes import SimpleTheme

theme = SimpleTheme()
theme.layout = "horizontal"
theme.sidebar_width = "20%"

admin.theme = theme
```

**22. Add a custom branding logo to the admin interface**

```python
admin.branding_logo = "/static/logo.png"
```

**23. Customize the branding logo's size**

```python
admin.branding_logo_size = "50px"
```

**24. Add a custom branding name to the admin interface**

```python
admin.branding_name = "My Admin Interface"
```

**25. Customize the branding name's color**

```python
admin.branding_name_color = "#007bff"
```

**26. Add a custom favicon to the admin interface**

```python
admin.favicon = "/static/favicon.ico"
```

**27. Customize the favicon's size**

```python
admin.favicon_size = "32px"
```

**28. Add a custom background image to the login page**

```python
admin.login_background_image = "/static/login-background.jpg"
```

**29. Customize the login background image's size**

```python
admin.login_background_image_size = "cover"
```

**30. Add a custom CSS file to the admin interface**

```python
admin.add_css_file("/static/custom.css")
```

**31. Add a custom JavaScript file to the admin interface**

```python
admin.add_js_file("/static/custom.js")
```

**32. Add a custom view to the login page**

```python
@admin.add_login_view("tos")
def terms_of_service(request):
    return templates.TemplateResponse("tos.html")
```

**33. Add a custom view to the logout page**

```python
@admin.add_logout_view("goodbye")
def goodbye_view(request):
    return templates.TemplateResponse("goodbye.html")
```

**34. Add a custom error handler to the admin interface**

```python
@admin.add_error_handler(404)
def not_found_error(request):
    return templates.TemplateResponse("404.html")
```

**35. Add a custom template context processor to the admin interface**

```python
@admin.add_template_context_processor
def my_template_context_processor(request):
    return {"my_custom_variable": "value"}
```

**36. Add a custom middleware to the admin interface**

```python
from fastapi.middleware.cors import CORSMiddleware

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
```
