simpleenergy / django-rest-framework-multi-slug-field

A serializer field for representing a relationship via a combination of fields on the target.
BSD 3-Clause "New" or "Revised" License
8 stars 0 forks source link

slug_field required #6

Closed bertonoronha closed 9 years ago

bertonoronha commented 9 years ago

Hello there,

Thank you for taking the time to develop this extension! However, I have a problem when trying to use multiple slug fields.

I add the requirement in the serializers.py:

from rest_framework_msf.fields import MultiSlugRelatedField

Then I add two fields in the slug_fields argument, and I get an error saying that The slug_field argument is required.

Am I doing something wrong? I followed the documentation, but maybe I missed something.

Thanks!

pipermerriam commented 9 years ago

Can you give me a code example that causes this error?

bertonoronha commented 9 years ago

I add:

from rest_framework_msf.fields import MultiSlugRelatedField

class IExampleSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.IntegerField(read_only=True)

    metabolites = serializers.SlugRelatedField(
        many = True,
        read_only = True,
        slug_fields = ('met_id','abbreviation')
    )

    class Meta:
        model = IEM
pipermerriam commented 9 years ago

Still not sure I have enough to diagnose this. Can you show me some code that uses this serializer and produces the error, or a copy/paste of the full stacktrace?

bertonoronha commented 9 years ago
class IEMSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField(read_only=True)

metabolites = serializers.SlugRelatedField(
    many = True,
    read_only = True,
    slug_fields = ('met_id','abbreviation')
)
class Meta:
    model = IEM

This is the serializer that I am using in this view:

class IEMViewSet(viewsets.ModelViewSet):

  queryset = IEM.objects.all()
  serializer_class = IEMSerializer
  filter_backends = (filters.SearchFilter,)
  search_fields = ('name',)

urls registered as:

router.register(r'iems', views.IEMViewSet)

When I try to access the page via localhost:8000/iems/ the stacktrace I get is this:

AssertionError at /iems/
Request Method: GET
Request URL:    http://localhost:8000/iems/
Django Version: 1.7.1
Exception Type: AssertionError
Exception Value:    
The `slug_field` argument is required.
 Exception Location:    C:\Users\alberto.noronha\AppData\Local\Continuum\Anaconda\lib\site-packages\rest_framework\relations.py in __init__, line 300
Python Executable:  C:\Users\alberto.noronha\AppData\Local\Continuum\Anaconda\python.exe
Python Version: 2.7.7
...

Stacktrace: http://dpaste.com/32Y786Q

I can post more code if needed, and thanks for the help!

pipermerriam commented 9 years ago

Hilarious, I completely overlooked it. You are not actually using the MultiSlugRelatedField. You're using the built-in SlugRelatedField.

Lemme know if that doesn't fix it. Marking this closed.

bertonoronha commented 9 years ago

Yes, I thought that was it was supposed to be as it is like that in the documentation. . When I use it I get an error like:

 'module' object has no attribute 'MultiSlugRelatedField'

I guess this is a problem with the import then.

pipermerriam commented 9 years ago

Yes, docs may be wrong. Correct import should be from rest_framework_msf.fields import MultiSlugRelatedField