# Strawberry GraphQL Django integration
[](https://github.com/strawberry-graphql/strawberry-django/actions/workflows/tests.yml)
[](https://codecov.io/gh/strawberry-graphql/strawberry-django)
[](https://pypi.org/project/strawberry-graphql-django/)
[](https://pepy.tech/project/strawberry-graphql-django)

[**Docs**](https://strawberry.rocks/docs/django) | [**Discord**](https://strawberry.rocks/discord)
This package provides powerful tools to generate GraphQL types, queries,
mutations and resolvers from Django models.
Installing `strawberry-graphql-django` package from the python package repository.
```shell
pip install strawberry-graphql-django
```
## Supported Features
- [x] GraphQL type generation from models
- [x] Filtering, pagination and ordering
- [x] Basic create, retrieve, update and delete (CRUD) types and mutations
- [x] Basic Django auth support, current user query, login and logout mutations
- [x] Django sync and async views
- [x] Permission extension using django's permissioning system
- [x] Relay support with automatic resolvers generation
- [x] Query optimization to improve performance and avoid common pitfalls (e.g n+1)
- [x] Debug Toolbar integration with graphiql to display metrics like SQL queries
- [x] Unit test integration
## Basic Usage
```python
# models.py
from django.db import models
class Fruit(models.Model):
"""A tasty treat"""
name = models.CharField(
max_length=20,
)
color = models.ForeignKey(
"Color",
on_delete=models.CASCADE,
related_name="fruits",
blank=True,
null=True,
)
class Color(models.Model):
name = models.CharField(
max_length=20,
help_text="field description",
)
```
```python
# types.py
import strawberry_django
from strawberry import auto
from . import models
@strawberry_django.type(models.Fruit)
class Fruit:
id: auto
name: auto
color: 'Color'
@strawberry_django.type(models.Color)
class Color:
id: auto
name: auto
fruits: list[Fruit]
```
```python
# schema.py
import strawberry
import strawberry_django
from strawberry_django.optimizer import DjangoOptimizerExtension
from .types import Fruit
@strawberry.type
class Query:
fruits: list[Fruit] = strawberry_django.field()
schema = strawberry.Schema(
query=Query,
extensions=[
DjangoOptimizerExtension, # not required, but highly recommended
],
)
```
```python
# urls.py
from django.urls import include, path
from strawberry.django.views import AsyncGraphQLView
from .schema import schema
urlpatterns = [
path('graphql', AsyncGraphQLView.as_view(schema=schema)),
]
```
Code above generates following schema.
```graphql
"""
A tasty treat
"""
type Fruit {
id: ID!
name: String!
color: Color
}
type Color {
id: ID!
"""
field description
"""
name: String!
fruits: [Fruit!]
}
type Query {
fruits: [Fruit!]!
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/strawberry-graphql/strawberry-django",
"name": "strawberry-graphql-django",
"maintainer": "Thiago Bellini Ribeiro",
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": "thiago@bellini.dev",
"keywords": "graphql, api, django, strawberry-graphql",
"author": "Lauri Hintsala",
"author_email": "lauri.hintsala@verkkopaja.fi",
"download_url": "https://files.pythonhosted.org/packages/53/e0/d8b74d1ecf1bce8ff80923d84fad3c277ee15e506038cd04fdb49ec87df1/strawberry_graphql_django-0.55.2.tar.gz",
"platform": null,
"description": "# Strawberry GraphQL Django integration\n\n[](https://github.com/strawberry-graphql/strawberry-django/actions/workflows/tests.yml)\n[](https://codecov.io/gh/strawberry-graphql/strawberry-django)\n[](https://pypi.org/project/strawberry-graphql-django/)\n[](https://pepy.tech/project/strawberry-graphql-django)\n\n\n[**Docs**](https://strawberry.rocks/docs/django) | [**Discord**](https://strawberry.rocks/discord)\n\nThis package provides powerful tools to generate GraphQL types, queries,\nmutations and resolvers from Django models.\n\nInstalling `strawberry-graphql-django` package from the python package repository.\n\n```shell\npip install strawberry-graphql-django\n```\n\n## Supported Features\n\n- [x] GraphQL type generation from models\n- [x] Filtering, pagination and ordering\n- [x] Basic create, retrieve, update and delete (CRUD) types and mutations\n- [x] Basic Django auth support, current user query, login and logout mutations\n- [x] Django sync and async views\n- [x] Permission extension using django's permissioning system\n- [x] Relay support with automatic resolvers generation\n- [x] Query optimization to improve performance and avoid common pitfalls (e.g n+1)\n- [x] Debug Toolbar integration with graphiql to display metrics like SQL queries\n- [x] Unit test integration\n\n## Basic Usage\n\n```python\n# models.py\n\nfrom django.db import models\n\nclass Fruit(models.Model):\n \"\"\"A tasty treat\"\"\"\n name = models.CharField(\n max_length=20,\n )\n color = models.ForeignKey(\n \"Color\",\n on_delete=models.CASCADE,\n related_name=\"fruits\",\n blank=True,\n null=True,\n )\n\nclass Color(models.Model):\n name = models.CharField(\n max_length=20,\n help_text=\"field description\",\n )\n```\n\n```python\n# types.py\n\nimport strawberry_django\nfrom strawberry import auto\n\nfrom . import models\n\n@strawberry_django.type(models.Fruit)\nclass Fruit:\n id: auto\n name: auto\n color: 'Color'\n\n@strawberry_django.type(models.Color)\nclass Color:\n id: auto\n name: auto\n fruits: list[Fruit]\n```\n\n```python\n# schema.py\n\nimport strawberry\nimport strawberry_django\nfrom strawberry_django.optimizer import DjangoOptimizerExtension\n\nfrom .types import Fruit\n\n@strawberry.type\nclass Query:\n fruits: list[Fruit] = strawberry_django.field()\n\nschema = strawberry.Schema(\n query=Query,\n extensions=[\n DjangoOptimizerExtension, # not required, but highly recommended\n ],\n)\n```\n\n```python\n# urls.py\n\nfrom django.urls import include, path\nfrom strawberry.django.views import AsyncGraphQLView\n\nfrom .schema import schema\n\nurlpatterns = [\n path('graphql', AsyncGraphQLView.as_view(schema=schema)),\n]\n```\n\nCode above generates following schema.\n\n```graphql\n\"\"\"\nA tasty treat\n\"\"\"\ntype Fruit {\n id: ID!\n name: String!\n color: Color\n}\n\ntype Color {\n id: ID!\n \"\"\"\n field description\n \"\"\"\n name: String!\n fruits: [Fruit!]\n}\n\ntype Query {\n fruits: [Fruit!]!\n}\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Strawberry GraphQL Django extension",
"version": "0.55.2",
"project_urls": {
"Documentation": "https://strawberry.rocks/docs/django",
"Homepage": "https://github.com/strawberry-graphql/strawberry-django",
"Repository": "https://github.com/strawberry-graphql/strawberry-django"
},
"split_keywords": [
"graphql",
" api",
" django",
" strawberry-graphql"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "87044aa1e54d407c24e693942aa66bf1fa20ca51dfd10790bef5c10cc07abc96",
"md5": "ff15c074c360a9a9a8780f419847a5ed",
"sha256": "38d280aba12cbb3c4727dd7380fe96b6de4731cebff1120ee46c97bc4ae03e82"
},
"downloads": -1,
"filename": "strawberry_graphql_django-0.55.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ff15c074c360a9a9a8780f419847a5ed",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 95742,
"upload_time": "2025-02-12T16:43:09",
"upload_time_iso_8601": "2025-02-12T16:43:09.947135Z",
"url": "https://files.pythonhosted.org/packages/87/04/4aa1e54d407c24e693942aa66bf1fa20ca51dfd10790bef5c10cc07abc96/strawberry_graphql_django-0.55.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "53e0d8b74d1ecf1bce8ff80923d84fad3c277ee15e506038cd04fdb49ec87df1",
"md5": "79097cc4042e35414d4346494f171bf5",
"sha256": "55af9627849843794f0abfc9939451b9af2d9918828be1a0e3722129b09e3d22"
},
"downloads": -1,
"filename": "strawberry_graphql_django-0.55.2.tar.gz",
"has_sig": false,
"md5_digest": "79097cc4042e35414d4346494f171bf5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 76450,
"upload_time": "2025-02-12T16:43:11",
"upload_time_iso_8601": "2025-02-12T16:43:11.844236Z",
"url": "https://files.pythonhosted.org/packages/53/e0/d8b74d1ecf1bce8ff80923d84fad3c277ee15e506038cd04fdb49ec87df1/strawberry_graphql_django-0.55.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-12 16:43:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "strawberry-graphql",
"github_project": "strawberry-django",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "strawberry-graphql-django"
}