drf-forms / drf-schema-adapter

Making using Django with frontend libraries and frameworks DRYer
http://drf-schema-adapter.readthedocs.io/en/latest/
MIT License
157 stars 40 forks source link

TypeError: 'NoneType' object is not callable at serializer_instance = view.serializer_class() #21

Closed Eloi-Marin closed 6 years ago

Eloi-Marin commented 7 years ago

When setting:

REST_FRAMEWORK = { 'DEFAULT_METADATA_CLASS': 'drf_auto_endpoint.metadata.AutoMetadata', } DRF_AUTO_METADATA_ADAPTER = 'drf_auto_endpoint.adapters.AngularFormlyAdapter'

and using a ModelViewSet instead of auto_endpoint with a custom get_serializer_class, eg:

`class MyViewSet(viewsets.ModelViewSet): def get_queryset(self): model = eval(self.kwargs.get('string')) return model.objects.all()

def get_serializer_class(self):
     MySerializer.Meta.model = eval(self.kwargs.get('string'))
     MySerializer.Meta.fields = '__all__'
     return MySerializer

class MySerializer(serializers.ModelSerializer): class Meta(): model = None`

where I have set the url pattern:

url(r'^api/(?P[\w-]+)/$',MyViewSet.as_view({'get':'list'})),

http request GET works, however when I request OPTIONS I get an internal server error:

File ".../drf_auto_endpoint/metadata.py", line 54, in determine_metadata serializer_instance = view.serializer_class() TypeError: 'NoneType' object is not callable

My goal is setting up a view where I can run an OPTIONS request for any model, given its name as a parameter in the URL.

nanuxbe commented 6 years ago

Hi, I uploaded a fix for your issue, updating to the latest version should now work for you.

On a side-note regarding what you are trying to do: your solution will not work unless you import all your models where you define this viewset. In order to avoid import them and to make your code more secure, you should checkout https://docs.djangoproject.com/en/1.11/ref/utils/#django.utils.module_loading.import_string

nanuxbe commented 6 years ago

I am closing this issue or now, if your problem is not fixed, feel free to re-open it