graphene-django-cud icon indicating copy to clipboard operation
graphene-django-cud copied to clipboard

Choices Field created via models.TextChoices(or its analogues) class

Open nikitka5702 opened this issue 2 years ago • 2 comments

python version 3.10.3
django version 4.0.3
graphene-django from main branch latest commit by date of posting(f6ec0689c18929344c79ae363d2e3d5628fa4a2d)
graphene_django_cud version 0.10.0

I was trying to create a DjangoCreateMutation(actually it fails for any kind of mutation) with this model

from django.db import models
from django.contrib.auth import get_user_model


User = get_user_model()


class Staff(models.Model):
    class StaffStatus(models.TextChoices):
        WORKING = 'working', 'Working'
        SUSPENDED = 'suspended', 'Suspended'
        FIRED = 'fired', 'Fired'

    first_name = models.CharField(max_length=64)
    middle_name = models.CharField(max_length=64)
    last_name = models.CharField(max_length=64)
    phone = models.CharField(max_length=32)
    email = models.CharField(max_length=256)
    position = models.CharField(max_length=64)
    address = models.CharField(max_length=512)
    status = models.CharField(max_length=16, choices=StaffStatus.choices, default=StaffStatus.WORKING)
    is_admin = models.BooleanField(default=False)

    user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, related_name='staff')

    def __str__(self):
        return f'{self.last_name} {self.first_name}({self.id})'

I suppose most of the fields can be skipped except the one with choices which was created via class _(models.TextChoices) or its analogues in it.

And when I tried to run app it fails with message

File "lib\site-packages\graphene_django_cud\converter.py", line 116, in convert_django_field_with_choices     
    from_registry.kwargs['description'] = field.help_text
AttributeError: 'BlankValueField' object has no attribute 'kwargs'. Did you mean: 'args'?

nikitka5702 avatar Mar 20 '22 09:03 nikitka5702

I managed to fix this by changing graphene_django_cud\converter.py:

            if from_registry:
                if hasattr(from_registry, 'kwargs'):
                    from_registry.kwargs['description'] = field.help_text
                    from_registry.kwargs['required'] = is_required(field, required)
                else:
                    from_registry.description = field.help_text
                    from_registry.required = is_required(field, required)
                return from_registry

goJesscho avatar Mar 23 '22 09:03 goJesscho

python version 3.10.3
django version 4.0.3
graphene-django from main branch latest commit by date of posting(f6ec0689c18929344c79ae363d2e3d5628fa4a2d)
graphene_django_cud version 0.10.0

I was trying to create a DjangoCreateMutation(actually it fails for any kind of mutation) with this model

from django.db import models
from django.contrib.auth import get_user_model


User = get_user_model()


class Staff(models.Model):
    class StaffStatus(models.TextChoices):
        WORKING = 'working', 'Working'
        SUSPENDED = 'suspended', 'Suspended'
        FIRED = 'fired', 'Fired'

    first_name = models.CharField(max_length=64)
    middle_name = models.CharField(max_length=64)
    last_name = models.CharField(max_length=64)
    phone = models.CharField(max_length=32)
    email = models.CharField(max_length=256)
    position = models.CharField(max_length=64)
    address = models.CharField(max_length=512)
    status = models.CharField(max_length=16, choices=StaffStatus.choices, default=StaffStatus.WORKING)
    is_admin = models.BooleanField(default=False)

    user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, related_name='staff')

    def __str__(self):
        return f'{self.last_name} {self.first_name}({self.id})'

I suppose most of the fields can be skipped except the one with choices which was created via class _(models.TextChoices) or its analogues in it.

And when I tried to run app it fails with message

File "lib\site-packages\graphene_django_cud\converter.py", line 116, in convert_django_field_with_choices     
    from_registry.kwargs['description'] = field.help_text
AttributeError: 'BlankValueField' object has no attribute 'kwargs'. Did you mean: 'args'?

you have this issue because you have graphene-django==3.0.0b7. if you downgrade it to graphene-django==2.15.0 you will not have this issue

NwawelAIroume avatar Jun 08 '22 12:06 NwawelAIroume