File: choices-field.md

package info (click to toggle)
strawberry-graphql-django 0.67.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,988 kB
  • sloc: python: 27,682; sh: 20; makefile: 20
file content (49 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
title: Django Choices Field
---

# django-choices-field

This lib provides integration for enum resolution for
[Django's TextChoices/IntegerChoices](https://docs.djangoproject.com/en/4.2/ref/models/fields/#enumeration-types)
when defining the fields using the
[django-choices-field](https://github.com/bellini666/django-choices-field) lib:

```python title="models.py"
from django.db import models
from django_choices_field import TextChoicesField

class Status(models.TextChoices):
    ACTIVE = "active", "Is Active"
    INACTIVE = "inactive", "Inactive"

class Company(models.Model):
    status = TextChoicesField(
        choices_enum=Status,
        default=Status.ACTIVE,
    )
```

```python title="types.py"
import strawberry
import strawberry_django

import .models

@strawberry_django.type(models.Company)
class Company:
    status: strawberry.auto
```

The code above would generate the following schema:

```graphql title="schema.graphql"
enum Status {
  ACTIVE
  INACTIVE
}

type Company {
  status: Status
}
```