Closed
Description
To reproduce this problem, create a Django model with a choices field with blank=True
:
class MyModel(models.Model):
PERIODIC_INTERVAL_CHOICES = (('Weekly', 'Weekly'),
('Bi-Weekly', 'Bi-Weekly'),
('Monthly', 'Monthly'),
('Quarterly', 'Quarterly'),
('Semi-Annually', 'Semi-Annually'),
'Annually', 'Annually'))
payment_frequency = models.CharField(
blank=True,
choices=PERIODIC_INTERVAL_CHOICES,
max_length=13)
The schema generated by DjangoObjectType will show incorrectly show that this field payment_frequency
is required. This is incorrect -- the expected behavior is that it NOT required.
I have been able to fix this issue by patching how the value of required is determined in graphene.converter.convert_django_field_with_choices
(shown for graphene-django 2.0.0) :
def convert_django_field_with_choices(field, registry=None):
# Modified from graphene_django.converter import convert_django_field_with_choices
# to adjust "required"
choices = getattr(field, 'choices', None)
if choices:
meta = field.model._meta
name = to_camel_case('{}_{}'.format(meta.object_name, field.name))
choices = list(get_choices(choices))
named_choices = [(c[0], c[1]) for c in choices]
named_choices_descriptions = {c[0]: c[2] for c in choices}
class EnumWithDescriptionsType(object):
@property
def description(self):
return named_choices_descriptions[self.name]
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
required = not (field.blank or field.null or field.default) # MODIFIED FROM ORIGINAL
return enum(description=field.help_text, required=required)
return convert_django_field(field, registry)
Metadata
Metadata
Assignees
Labels
No labels