mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-28 12:33:43 +03:00
Implementing reading form fields
This commit is contained in:
parent
ade2918d21
commit
f9a4305e61
|
@ -5,7 +5,7 @@ import graphene
|
||||||
from graphene_django import DjangoObjectType
|
from graphene_django import DjangoObjectType
|
||||||
|
|
||||||
from ...tests.models import CHOICES, Film, Reporter
|
from ...tests.models import CHOICES, Film, Reporter
|
||||||
from ..types import DjangoFormFieldObjectType, DjangoFormInputObjectType, DjangoFormObjectType
|
from ..types import DjangoFormFieldObjectType, DjangoFormInputObjectType, DjangoFormObjectType, DjangoFormTypeOptions
|
||||||
|
|
||||||
# Reporter a_choice CHOICES = ((1, "this"), (2, _("that")))
|
# Reporter a_choice CHOICES = ((1, "this"), (2, _("that")))
|
||||||
THIS = CHOICES[0][0]
|
THIS = CHOICES[0][0]
|
||||||
|
@ -40,41 +40,16 @@ class MyForm(forms.Form):
|
||||||
|
|
||||||
|
|
||||||
class ReporterFormType(DjangoFormObjectType):
|
class ReporterFormType(DjangoFormObjectType):
|
||||||
class Meta:
|
form_class = ReporterForm
|
||||||
form_class = ReporterForm
|
only_fields = ('pets', 'email')
|
||||||
|
|
||||||
|
|
||||||
def test_needs_form_class():
|
|
||||||
with raises(Exception) as exc:
|
|
||||||
class MyFormType(DjangoFormObjectType):
|
|
||||||
pass
|
|
||||||
|
|
||||||
assert exc.value.args[0] == "form_class is required for DjangoFormObjectType"
|
|
||||||
|
|
||||||
|
|
||||||
def test_type_form_has_fields():
|
|
||||||
class ReporterFormType(DjangoFormObjectType):
|
|
||||||
class Meta:
|
|
||||||
form_class = ReporterForm
|
|
||||||
only_fields = ("first_name", "last_name", "a_choice")
|
|
||||||
|
|
||||||
fields = ["first_name", "last_name", "a_choice", "id"]
|
|
||||||
assert all(f in ReporterFormType._meta.fields for f in fields)
|
|
||||||
|
|
||||||
|
|
||||||
def test_type_form_has_fields():
|
|
||||||
class MyFormFieldType(DjangoFormFieldObjectType):
|
|
||||||
class Meta:
|
|
||||||
form_class = MyForm
|
|
||||||
|
|
||||||
fields = ["text_field", "int_field", "id"]
|
|
||||||
assert all(f in MyFormFieldType._meta.fields for f in fields)
|
|
||||||
|
|
||||||
|
|
||||||
def test_query_djangoformtype():
|
def test_query_djangoformtype():
|
||||||
class MyFormType(DjangoFormObjectType):
|
class MyFormType(DjangoFormObjectType):
|
||||||
class Meta:
|
form_class = MyForm
|
||||||
form_class = MyForm
|
|
||||||
|
only_fields = ('text_field', 'int_field')
|
||||||
|
exclude_fields = []
|
||||||
|
|
||||||
class MockQuery(graphene.ObjectType):
|
class MockQuery(graphene.ObjectType):
|
||||||
form = graphene.Field(
|
form = graphene.Field(
|
||||||
|
@ -106,11 +81,11 @@ def test_query_djangoformtype():
|
||||||
"fields": [
|
"fields": [
|
||||||
{
|
{
|
||||||
"name": "text_field",
|
"name": "text_field",
|
||||||
"type": "CharField"
|
"type": "String"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "int_field",
|
"name": "int_field",
|
||||||
"type": "IntegerField"
|
"type": "Int"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,9 @@ from graphene.utils.str_converters import to_camel_case
|
||||||
from django.db.models import Model
|
from django.db.models import Model
|
||||||
|
|
||||||
from ..converter import BlankValueField
|
from ..converter import BlankValueField
|
||||||
from ..types import ErrorType # noqa Import ErrorType for backwards compatibility
|
from ..types import DjangoObjectTypeOptions, ErrorType # noqa Import ErrorType for backwards compatibility
|
||||||
from .mutation import fields_for_form
|
from .mutation import fields_for_form
|
||||||
|
from graphene.types.objecttype import ObjectTypeOptions
|
||||||
|
|
||||||
|
|
||||||
class DjangoFormFieldObjectType(ObjectType):
|
class DjangoFormFieldObjectType(ObjectType):
|
||||||
|
@ -24,50 +25,35 @@ class DjangoFormFieldObjectType(ObjectType):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class DjangoFormTypeOptions(ObjectTypeOptions):
|
||||||
|
form_class = Form
|
||||||
|
only_fields = ()
|
||||||
|
exclude_fields = ()
|
||||||
|
object_type = Model
|
||||||
|
|
||||||
|
|
||||||
class DjangoFormObjectType(ObjectType):
|
class DjangoFormObjectType(ObjectType):
|
||||||
class Meta:
|
form_class = Form
|
||||||
form_class = Form
|
only_fields = ()
|
||||||
object_type = Model
|
exclude_fields = ()
|
||||||
only_fields = []
|
|
||||||
exclude_fields = []
|
|
||||||
|
|
||||||
fields = graphene.List(
|
fields = graphene.List(
|
||||||
DjangoFormFieldObjectType
|
DjangoFormFieldObjectType
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def __init_subclass_with_meta__(
|
|
||||||
cls,
|
|
||||||
container=None,
|
|
||||||
_meta=None,
|
|
||||||
only_fields=(),
|
|
||||||
exclude_fields=(),
|
|
||||||
form_class=None,
|
|
||||||
object_type=None,
|
|
||||||
add_id_field_name=None,
|
|
||||||
add_id_field_type=None,
|
|
||||||
**options,
|
|
||||||
):
|
|
||||||
|
|
||||||
if not form_class:
|
|
||||||
raise Exception("form_class is required for DjangoFormObjectType")
|
|
||||||
|
|
||||||
super().__init_subclass_with_meta__(container=container, _meta=_meta, **options)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolve_fields(parent, info):
|
def resolve_fields(parent, info):
|
||||||
form_class = parent._meta.form_class
|
form_class = parent.form_class
|
||||||
form = form_class()
|
form = form_class()
|
||||||
only_fields = parent._meta.only_fields
|
only_fields = parent.only_fields
|
||||||
exclude_fields = parent._meta.exclude_fields
|
exclude_fields = parent.exclude_fields
|
||||||
|
|
||||||
form_fields = fields_for_form(form, only_fields, exclude_fields)
|
form_fields = fields_for_form(form, only_fields, exclude_fields)
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
type = field.__class__.__name__
|
|
||||||
|
|
||||||
for name, field in form_fields.items():
|
for name, field in form_fields.items():
|
||||||
|
type = field.__class__.__name__
|
||||||
result.append(
|
result.append(
|
||||||
DjangoFormFieldObjectType(
|
DjangoFormFieldObjectType(
|
||||||
name=name,
|
name=name,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user