tkhyn / django-gm2m

MIT License
36 stars 23 forks source link

Django REST Framework doesn't render ModelViewSet with GM2MField #43

Open tkhyn opened 5 years ago

tkhyn commented 5 years ago

Original report by Nikita (Bitbucket: konyshevn, GitHub: konyshevn).


I am using Django REST Framework and django-gm2m application:

models.py

#!python

class DocIncome(models.Model):
    doc_date = models.DateTimeField()
    doc_num = models.IntegerField(unique_for_date='doc_date')
    follower = GM2MField()

serializers.py

#!python

class DocIncomeSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.DocIncome
        exclude = ()

views.py

#!python

class DocIncomeViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.DocIncomeSerializer
    queryset = DocIncome.objects.all()

But I open api url I get the error -

Exception Value: 'GM2MField' object has no attribute 'get_limit_choices_to'

Exception Location: C:\Users\konyshev.na\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\utils\field_mapping.py in get_relation_kwargs, line 252

Does anyone know how to fix it?

tkhyn commented 5 years ago

Original comment by Sharo Qadir (Bitbucket: sharlock93, GitHub: sharlock93).


I have the same problem,

#!python

get_limit_choices_to

most likely is used to get

#!python

limit_choices_to

attribute from django as its used in foreign_relations when rendering for ModelForm

Limit choices

how it is suppose to work with gm2m and rest framework is something I haven't thought about. but I fixed this issue temporarily by making a new class that extends GM2MField

#!python

from gm2m import GM2MField as gm2m_field
class GM2MField(gm2m_field):
    def get_limit_choices_to(self):
        pass

this you can put at the top of your models.py file and leave the rest of the code as is.

tkhyn commented 5 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Hi,

The limit_choices_to feature is not implemented yet in django-gm2m (see the docs). As I have not used django-gm2m for more than a year (nor django at all), and my time is very limited at the moment, it is unlikely I'll have the time to implement it.

However, I could probably think of a temporary fix just to avoid the exception as per @sharlock93's suggestion. Would you be happy with that?

tkhyn commented 5 years ago

Original comment by Nikita (Bitbucket: konyshevn, GitHub: konyshevn).


Hi, I fixed it like @Sharo Qadir. That's enough, thank you.