graphql-python / graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
http://docs.graphene-python.org/projects/django/en/latest/
MIT License
4.28k stars 766 forks source link

Don't know how to convert the Django field class 'djongo.models.fields.ArrayField' #1043

Open sfahad1414 opened 3 years ago

sfahad1414 commented 3 years ago

from djongo.models import Model, CharField, ObjectIdField, IntegerField, ArrayField from django.forms import ModelForm

class Contact(Model): name = CharField(max_length=50) phone_number = IntegerField()

class Meta:
    abstract = True

class ContactForm(ModelForm): class Meta: model = Contact fields = ("name", "phone_number")

class Organization(Model): _id = ObjectIdField() name = CharField(max_length=50, unique=True) desk_number = IntegerField() registered_address = CharField(max_length=256) communication_address = CharField(max_length=256) email_address = CharField(max_length=150) contacts = ArrayField(model_container=Contact, model_form_class=ContactForm, null=False) type = CharField(max_length=256) pan_number = CharField(max_length=15) gst_number = CharField(max_length=25) member_count = IntegerField() account_number = IntegerField() bank_name = CharField(70) ifsc_code = CharField(10) registration_year = CharField(4)

class Meta:
    """
    to set table name in database
    """
    db_table = "organization"

File "D:\miniconda\envs\deeplearning\lib\threading.py", line 932, in _bootstrap_inner self.run() File "D:\miniconda\envs\deeplearning\lib\threading.py", line 870, in run self._target(*self._args, self._kwargs) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper fn(*args, kwargs) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run self.check(display_num_errors=True) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\core\management\base.py", line 392, in check all_issues = self._run_checks( File "D:\miniconda\envs\deeplearning\lib\site-packages\django\core\management\base.py", line 382, in _run_checks return checks.run_checks(kwargs) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks new_errors = check(app_configs=app_configs) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config return check_resolver(resolver) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "D:\miniconda\envs\deeplearning\lib\site-packages\django\urls\resolvers.py", line 407, in check for pattern in self.url_patterns: File "D:\miniconda\envs\deeplearning\lib\site-packages\django\utils\functional.py", line 48, in get res = instance.dict[self.name] = self.func(instance) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\utils\functional.py", line 48, in get res = instance.dict[self.name] = self.func(instance) File "D:\miniconda\envs\deeplearning\lib\site-packages\django\urls\resolvers.py", line 581, in urlconf_module return import_module(self.urlconf_name) File "D:\miniconda\envs\deeplearning\lib\importlib__init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked File "", line 783, in exec_module File "", line 219, in _call_with_frames_removed File "D:\Work\datascience\deeplearning\main\urls.py", line 26, in from .schema import schema File "D:\Work\datascience\deeplearning\main\schema.py", line 14, in from api.organization.query import Query as QOrganization File "D:\Work\datascience\deeplearning\api\organization\query.py", line 15, in class OrganizationType(DjangoObjectType): File "D:\miniconda\envs\deeplearning\lib\site-packages\graphene\utils\subclass_with_meta.py", line 52, in __init_subclass super_class.init_subclass_with_meta__(**options) File "D:\miniconda\envs\deeplearning\lib\site-packages\graphene_django\types.py", line 227, in init_subclass_with_meta__ construct_fields(model, registry, fields, exclude, convert_choices_to_enum), File "D:\miniconda\envs\deeplearning\lib\site-packages\graphene_django\types.py", line 63, in construct_fields converted = convert_django_field_with_choices( File "D:\miniconda\envs\deeplearning\lib\site-packages\graphene_django\converter.py", line 112, in convert_django_field_with_choices converted = convert_django_field(field, registry) File "D:\miniconda\envs\deeplearning\lib\functools.py", line 875, in wrapper return dispatch(args[0].class)(*args, kw) File "D:\miniconda\envs\deeplearning\lib\site-packages\graphene_django\converter.py", line 120, in convert_django_field raise Exception( Exception: Don't know how to convert the Django field organization.Organization.contacts (<class 'djongo.models.fields.ArrayField'>)

zbyte64 commented 3 years ago

The workaround would be to register a custom handler: https://github.com/graphql-python/graphene-django/issues/18#issuecomment-253549577

But as this is now part of Django, it should be added.

tcleonard commented 3 years ago

The Django Postgres array field handler is already implemented.

The Mongodb arrayfield is not part of Django (it's in Djongo)... so I would say that implementing a custom handler is the solution (and honestly you can most likely use the same handler as the postgres arrayfield one)