mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-22 17:47:12 +03:00
Added required conversion to model and form fields. Fixed #2
This commit is contained in:
parent
7fbc3e69b1
commit
b8f9fec674
|
@ -47,7 +47,7 @@ def convert_django_field_with_choices(field, registry=None):
|
|||
return named_choices_descriptions[self.name]
|
||||
|
||||
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
|
||||
return enum(description=field.help_text)
|
||||
return enum(description=field.help_text, required=not field.null)
|
||||
return convert_django_field(field, registry)
|
||||
|
||||
|
||||
|
@ -67,12 +67,12 @@ def convert_django_field(field, registry=None):
|
|||
@convert_django_field.register(models.FileField)
|
||||
@convert_django_field.register(UUIDField)
|
||||
def convert_field_to_string(field, registry=None):
|
||||
return String(description=field.help_text)
|
||||
return String(description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.AutoField)
|
||||
def convert_field_to_id(field, registry=None):
|
||||
return ID(description=field.help_text)
|
||||
return ID(description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.PositiveIntegerField)
|
||||
|
@ -81,7 +81,7 @@ def convert_field_to_id(field, registry=None):
|
|||
@convert_django_field.register(models.BigIntegerField)
|
||||
@convert_django_field.register(models.IntegerField)
|
||||
def convert_field_to_int(field, registry=None):
|
||||
return Int(description=field.help_text)
|
||||
return Int(description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.BooleanField)
|
||||
|
@ -91,18 +91,18 @@ def convert_field_to_boolean(field, registry=None):
|
|||
|
||||
@convert_django_field.register(models.NullBooleanField)
|
||||
def convert_field_to_nullboolean(field, registry=None):
|
||||
return Boolean(description=field.help_text)
|
||||
return Boolean(description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.DecimalField)
|
||||
@convert_django_field.register(models.FloatField)
|
||||
def convert_field_to_float(field, registry=None):
|
||||
return Float(description=field.help_text)
|
||||
return Float(description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.DateField)
|
||||
def convert_date_to_string(field, registry=None):
|
||||
return DateTime(description=field.help_text)
|
||||
return DateTime(description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.OneToOneRel)
|
||||
|
@ -114,7 +114,7 @@ def convert_onetoone_field_to_djangomodel(field, registry=None):
|
|||
if not _type:
|
||||
return
|
||||
|
||||
return Field(_type)
|
||||
return Field(_type, required=not field.null)
|
||||
|
||||
return Dynamic(dynamic_type)
|
||||
|
||||
|
@ -149,7 +149,7 @@ def convert_relatedfield_to_djangomodel(field, registry=None):
|
|||
return
|
||||
|
||||
if isinstance(field.field, models.OneToOneField):
|
||||
return Field(_type)
|
||||
return Field(_type, required=not field.field.null)
|
||||
|
||||
if is_node(_type):
|
||||
return get_connection_field(_type)
|
||||
|
@ -168,7 +168,7 @@ def convert_field_to_djangomodel(field, registry=None):
|
|||
if not _type:
|
||||
return
|
||||
|
||||
return Field(_type, description=field.help_text)
|
||||
return Field(_type, description=field.help_text, required=not field.null)
|
||||
|
||||
return Dynamic(dynamic_type)
|
||||
|
||||
|
@ -178,13 +178,13 @@ def convert_postgres_array_to_list(field, registry=None):
|
|||
base_type = convert_django_field(field.base_field)
|
||||
if not isinstance(base_type, (List, NonNull)):
|
||||
base_type = type(base_type)
|
||||
return List(base_type, description=field.help_text)
|
||||
return List(base_type, description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(HStoreField)
|
||||
@convert_django_field.register(JSONField)
|
||||
def convert_posgres_field_to_string(field, registry=None):
|
||||
return JSONString(description=field.help_text)
|
||||
return JSONString(description=field.help_text, required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(RangeField)
|
||||
|
@ -192,4 +192,4 @@ def convert_posgres_range_to_string(field, registry=None):
|
|||
inner_type = convert_django_field(field.base_field)
|
||||
if not isinstance(inner_type, (List, NonNull)):
|
||||
inner_type = type(inner_type)
|
||||
return List(inner_type, description=field.help_text)
|
||||
return List(inner_type, description=field.help_text, required=not field.null)
|
||||
|
|
|
@ -34,13 +34,13 @@ def convert_form_field(field):
|
|||
@convert_form_field.register(forms.Field)
|
||||
@convert_form_field.register(UUIDField)
|
||||
def convert_form_field_to_string(field):
|
||||
return String(description=field.help_text)
|
||||
return String(description=field.help_text, required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.IntegerField)
|
||||
@convert_form_field.register(forms.NumberInput)
|
||||
def convert_form_field_to_int(field):
|
||||
return Int(description=field.help_text)
|
||||
return Int(description=field.help_text, required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.BooleanField)
|
||||
|
@ -56,16 +56,16 @@ def convert_form_field_to_nullboolean(field):
|
|||
@convert_form_field.register(forms.DecimalField)
|
||||
@convert_form_field.register(forms.FloatField)
|
||||
def convert_form_field_to_float(field):
|
||||
return Float(description=field.help_text)
|
||||
return Float(description=field.help_text, required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
||||
@convert_form_field.register(GlobalIDMultipleChoiceField)
|
||||
def convert_form_field_to_list(field):
|
||||
return List(ID)
|
||||
return List(ID, required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.ModelChoiceField)
|
||||
@convert_form_field.register(GlobalIDFormField)
|
||||
def convert_form_field_to_id(field):
|
||||
return ID()
|
||||
return ID(required=field.required)
|
||||
|
|
|
@ -226,24 +226,27 @@ def test_should_onetoone_reverse_convert_model():
|
|||
assert isinstance(graphene_field, graphene.Dynamic)
|
||||
dynamic_field = graphene_field.get_type()
|
||||
assert isinstance(dynamic_field, graphene.Field)
|
||||
assert dynamic_field.type == A
|
||||
assert isinstance(dynamic_field.type, graphene.NonNull)
|
||||
assert dynamic_field.of_type.type == A
|
||||
|
||||
|
||||
@pytest.mark.skipif(ArrayField is MissingType,
|
||||
reason="ArrayField should exist")
|
||||
def test_should_postgres_array_convert_list():
|
||||
field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100))
|
||||
assert isinstance(field.type, graphene.List)
|
||||
assert field.type.of_type == graphene.String
|
||||
assert isinstance(field.type, graphene.NonNull)
|
||||
assert isinstance(field.type.of_type, graphene.List)
|
||||
assert field.type.of_type.of_type == graphene.String
|
||||
|
||||
|
||||
@pytest.mark.skipif(ArrayField is MissingType,
|
||||
reason="ArrayField should exist")
|
||||
def test_should_postgres_array_multiple_convert_list():
|
||||
field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100)))
|
||||
assert isinstance(field.type, graphene.List)
|
||||
assert isinstance(field.type, graphene.NonNull)
|
||||
assert isinstance(field.type.of_type, graphene.List)
|
||||
assert field.type.of_type.of_type == graphene.String
|
||||
assert isinstance(field.type.of_type.of_type, graphene.List)
|
||||
assert field.type.of_type.of_type.of_type == graphene.String
|
||||
|
||||
|
||||
@pytest.mark.skipif(HStoreField is MissingType,
|
||||
|
@ -263,5 +266,6 @@ def test_should_postgres_json_convert_string():
|
|||
def test_should_postgres_range_convert_list():
|
||||
from django.contrib.postgres.fields import IntegerRangeField
|
||||
field = assert_conversion(IntegerRangeField, graphene.List)
|
||||
assert isinstance(field.type, graphene.List)
|
||||
assert field.type.of_type == graphene.Int
|
||||
assert isinstance(field.type, graphene.NonNull)
|
||||
assert isinstance(field.type.of_type, graphene.List)
|
||||
assert field.type.of_type.of_type == graphene.Int
|
||||
|
|
|
@ -63,10 +63,10 @@ schema {
|
|||
|
||||
type Article implements Node {
|
||||
id: ID!
|
||||
headline: String
|
||||
pubDate: DateTime
|
||||
reporter: Reporter
|
||||
lang: ArticleLang
|
||||
headline: String!
|
||||
pubDate: DateTime!
|
||||
reporter: Reporter!
|
||||
lang: ArticleLang!
|
||||
importance: ArticleImportance
|
||||
}
|
||||
|
||||
|
@ -104,12 +104,12 @@ type PageInfo {
|
|||
}
|
||||
|
||||
type Reporter {
|
||||
id: ID
|
||||
firstName: String
|
||||
lastName: String
|
||||
email: String
|
||||
id: ID!
|
||||
firstName: String!
|
||||
lastName: String!
|
||||
email: String!
|
||||
pets: [Reporter]
|
||||
aChoice: ReporterA_choice
|
||||
aChoice: ReporterA_choice!
|
||||
articles(before: String, after: String, first: Int, last: Int): ArticleConnection
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user