Custom lookup_field - how to

3,690 views
Skip to first unread message

Paul Walsh

unread,
May 9, 2013, 11:30:24 AM5/9/13
to django-res...@googlegroups.com
I have an API setup. 

And now, I decided to change from my pk (django ID field) to a uuid field (which I use on my models, precisely for such purposes).

Following what I think are the instructions for custom lookup fields, I get an exception, namely:

Could not resolve URL for field using view name "mymodel-detail"

Not sure what I am doing wrong, I know it must be my config, but I thought I followed the docs...

I do see that the error stems from relations.py, where it is still trying to use pk to resolve the URL of MyModel objects.

My setup:

#models.py
class MyModel():
    uuid = 
    # other stuff

#serializers.py
class MyModelSerializer(serializers.HyperlinkedModelSerializer):
    class Meta: 
        model = MyModel
        lookup_field = 'uuid'
        fields = ('url', 'id', 'uuid', 'image','first_name', 'last_name')
        read_only_fields = ('image',)

#views.py
class MyModelDetail(UserObjDetail):
    model = MyModel
    serializer_class = serializers.MyModelSerializer
    lookup_field = 'uuid'


#urls.py
# all the other stuff
url(r'^mymodels/(?P<uuid>[-\w]+)/, csrf_exempt(views.MyModelDetail.as_view()), name='mymodel-detail' ),





Tom Christie

unread,
May 9, 2013, 12:11:54 PM5/9/13
to django-res...@googlegroups.com
That's a bug right there.  HyperlinkedModelSerializer wasn't respecting `lookup_field`.
Now fixed in master by this commit.
If you get a chance to confirm that'd be great.
I'll probably be rolling a new release in the next day or two.

Cheers,


  Tom

Paul Walsh

unread,
May 9, 2013, 1:27:08 PM5/9/13
to django-res...@googlegroups.com
Ok, I am using tip on master.

It is still not working, and I am still confused as to whether I have an incorrect config, or there is still a bug.

Code:

#models.py
class Person():
    uuid =
    # other fields 


#serializers.py
class PersonSerializer(serializers.HyperlinkedModelSerializer):

    emails = PersonEmailSerializer(required=False)
    phones = PersonPhoneSerializer(required=False)
    addresses = PersonAddressSerializer(required=False)
    url = serializers.HyperlinkedIdentityField(view_name='api_v1_person_detail')

    class Meta:
        model = Person
        fields = ('url', 'id', 'uuid', 'image','first_name', 'last_name', 'emails',
                  'phones', 'addresses', 'created_on', 'last_modified')
        read_only_fields = ('image', 'emails', 'phones', 'addresses')

#views.py
class PersonDetail(UserObjDetail):
    """Person object detail endpoint"""

    model = Person
    serializer_class = serializers.PersonSerializer
    lookup_field = 'uuid'

#urls.py
    # other stuff
    url(r'^persons/(?P<uuid>[-\w]+)/$', 
        csrf_exempt(views.PersonDetail.as_view()),
        name='api_v1_person_detail'
    ),
    # other stuff

Paul Walsh

unread,
May 11, 2013, 2:18:21 PM5/11/13
to django-res...@googlegroups.com
Tom,

Is there any problem with my configuration here? 

Paul Walsh
0543551144


--
You received this message because you are subscribed to a topic in the Google Groups "django-rest-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/3TG8Iuk-NUM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tom Christie

unread,
May 12, 2013, 3:17:21 AM5/12/13
to django-res...@googlegroups.com
Is there any problem with my configuration here?

In the latest snippet you seem to have changed the URL name from 'mymodel-detail' to 'api_v1_person_detail', which wouldn't work.

As an aside, it's best not to address messages to me personally - there are plenty of other folks who may also be able to help out. :)


--
You received this message because you are subscribed to the Google Groups "django-rest-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

Paul Walsh

unread,
May 12, 2013, 3:22:43 AM5/12/13
to django-res...@googlegroups.com
Sure, I only addressed you personally because of the bug.

My new naming of the URL is not the issue, I have the same problem either way.

But, are you saying it is not possible for me to use a custom lookup in combination with my own URL naming scheme?

Paul Walsh
0543551144

Tom Christie

unread,
May 12, 2013, 5:59:00 AM5/12/13
to django-res...@googlegroups.com
> My new naming of the URL is not the issue, I have the same problem either way.

Okay, will try to prioritize that ticket,  I think there's a lack of tests around that area, so if you wanted to help move it on the best thing to do would be to try to pull together a failing test case.


> But, are you saying it is not possible for me to use a custom lookup in combination with my own URL naming scheme?

No, that's not the case. The url naming scheme you use doesn't have anything to do with if you're using non-pk lookup fields or not, *but* if you're not using the '{model}-detail' style then you'd need to set the view name on the serializer url field explicitly.  (Which is the case for either pk lookups or non-pk-lookups.)

Cheers,

  Tom

Paul Walsh
0543551144



Paul Walsh


To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "django-rest-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to a topic in the Google Groups "django-rest-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/3TG8Iuk-NUM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.

Hervé Le Roy

unread,
May 18, 2013, 5:59:08 PM5/18/13
to django-res...@googlegroups.com
I think I have the same issue as Paul.

Could not resolve URL for field using view name "note-detail"

# serializers.py
class NoteSerializer(serializers.HyperlinkedModelSerializer):
    
    class Meta:
        model = Note
        lookup_field = 'uuid'
        fields = ('url', 'uuid', 'is_deleted', 'last_modified', 'last_updated', 'updated_by', 'datetime', 'freetext')
        read_only_fields = ('is_deleted', 'last_updated',)

# views.py

class NoteViewSet(SyncViewSet):

    queryset = Note.objects.all()
    serializer_class = NoteSerializer
    lookup_field = 'uuid'

# urls.py

router = DefaultRouter()
router.register(r'notes', views.NoteViewSet)

urlpatterns = patterns('',
    url(r'^api/v0/', include(router.urls)),
    
)

Cheers,

Hervé

Paul Walsh

unread,
May 19, 2013, 3:33:44 AM5/19/13
to django-res...@googlegroups.com
I wanted to check this and write some tests to prove the problem, but I got stuck into another project. I hope to get back to this in the next week. It certainly does seem like we have a bug here.

Paul Walsh
0543551144


To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.

Mike C Leach

unread,
May 30, 2013, 7:05:16 PM5/30/13
to django-res...@googlegroups.com
Hello,

I think I had the same problem, and I seem to have solved it for my case. The fix for me was changing the serializer type from HyperlinkedModelSerializer to ModelSerializer. I realize this may not be possible for you, but if your case is indeed a bug, it seems it stems from something going wrong in relations.py in the HyperlinkedModelSerializer. Hope this helps.

Paul Walsh
0543551144



Paul Walsh



Paul Walsh


To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "django-rest-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to a topic in the Google Groups "django-rest-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/3TG8Iuk-NUM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tom Christie

unread,
May 31, 2013, 6:53:21 AM5/31/13
to django-res...@googlegroups.com
Now fixed in master, by this commit.

Paul Walsh

unread,
May 31, 2013, 8:45:27 AM5/31/13
to django-res...@googlegroups.com
Awesome Tom, thanks.
--
Paul Walsh
0543551144

Reply all
Reply to author
Forward
0 new messages