Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting started issues. #1249

Closed
qooleot opened this issue Nov 25, 2013 · 25 comments
Closed

Getting started issues. #1249

qooleot opened this issue Nov 25, 2013 · 25 comments

Comments

@qooleot
Copy link
Contributor

qooleot commented Nov 25, 2013

I'm a new user to django-rest-framework and ran into several issues, so I thought I'd post them here to see if any pieces of the quickstart docs should be updated to help others avoid the same.

  1. A quick first backwards-incompatible django change to consider updating for a new-users focused quickstart:

On django 1.6 the first line of the home page example blows up:

from django.conf.urls.defaults import url, patterns, include

needs to be:

from django.conf.urls import url, patterns, include

  1. The framework doesn't seem to work with django-admin.

I took the basic poll site with django 1.6 (I also tried 1.5.5 and ran into the same issues) with py3.3.2 and pip installed the framework, updated the settings.py as per home page, and completely overrode the urls.py with what was on the home page of django-rest-framework.

At first, everything didn't work due to:

In template /home/taras/Envs/django1/lib/python3.3/site-packages/rest_framework/templates/rest_framework/form.html, error at line 9

Could not resolve URL for hyperlinked relationship using view name "permission-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.

I tested in another browser where I was not logged into django-admin as an admin beforehand, and that worked perfectly.

I also tried this on another site, leaving the main urls.py file with /admin/ urls intact and had the same issue.

I tried changing the default permissions thinking that might be somehow related to:

'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),

and the non-logged browers got a correct json message saying credentials were not provided, and still a stack trace on the other.

Any help with #2 would be great, and let me know if you need additional info. Thanks!

@qooleot
Copy link
Contributor Author

qooleot commented Dec 1, 2013

#2 is still my primary issue, so I thought I'd try to put out as much info as possible. It seems really bizarre that django-rest-framework works perfectly unless I'm also running django-admin. The beginner tutorial would likely assume the user is taking something like the "Poll" django app, which means they'd be logged into django admin.

djangorestissue

(django1) taras@desktop:~/Projects/tutorialsite$ pip freeze
Django==1.6
Jinja2==2.7.1
Markdown==2.3.1
MarkupSafe==0.18
Pygments==1.6
South==0.8.4
Sphinx==1.2b1
bpython==0.12
coverage==3.7
distribute==0.7.3
django-braces==1.2.2
django-debug-toolbar==0.11.0
django-discover-runner==1.0
django-filter==0.7
django-grappelli==2.5.0
django-model-utils==1.5.0
djangorestframework==2.3.9
docutils==0.11
logutils==0.3.3
psycopg2==2.5.1
six==1.4.1
sqlparse==0.1.10

I'd be more than happy to write up an updated walkthrough for the community when I figure this out, so others will benefit by exploring this first-attempt issue. Thanks in advance for any help.

@tomchristie
Copy link
Member

Hiya,

Apologies for the delay.

I believe the issue here is that the quickstart example is actually broken in a non-obvious way - there's no view for permissions - if there are no permissions in the database that'll work fine, it never needs to generate URLs linnking to the permission instances, but if there are permission instances then it'll break in the ay you're seeing.

Looks like we need to tweak the example to either:

  • Add in a permission view (probably not as we'd like to keep it simple.)
  • Tweak the representations so they don't need a permission view.

@qooleot
Copy link
Contributor Author

qooleot commented Dec 8, 2013

Hey thanks, and my first issue was fixed by #1274. I managed to fix the primary issue adding permissions, so no rush for my own purposes on this.

@synotna
Copy link

synotna commented Jan 17, 2014

What permissions need to be added? Have the same problem

Relevant xkcd, http://xkcd.com/979/

@tomchristie
Copy link
Member

Currently breaks if you have an permission instances in Django's auth.permissions table.

@synotna
Copy link

synotna commented Jan 17, 2014

Odd, when I moved the API stuff out of my root project and into separate apps individually, it stopped being broken: I can use the API with or without logging in, and admin still works

Instead of putting everything in ///urls.py I'm using ///urls.py & api.py

@shacker
Copy link

shacker commented Jan 19, 2014

@synotna Moving Users-related code from urls.py and into an app did not fix it for me.

@tomchristie I've been working on this same issue for three hours and getting nowhere. I've got the lookup_field set, and the related model is all clearly defined as far as I can tell. Just can't seem to fix this error when visiting /api/users.

The code I'm using was copied from the sample project and then lightly modified. Here's what I've got as of now:

http://dpaste.com/1559234/

Reading StackOverflow answers to this and the docs isn't getting me there. Just can't see what I'm doing wrong. Thanks for any clues.

@tomchristie
Copy link
Member

@shacker Looks like you need to (also) set the lookup_field on the UserSerializer class. The viewset is using lookup_field but the serializer representation doesn't match up with that.

Also see: http://www.django-rest-framework.org/api-guide/serializers#how-hyperlinked-views-are-determined

We could certainly do with some easier way of helping users resolve the issue when hyperlinked serializations don't match up with the views being defined, but I'm not too sure how to approach it.

@shacker
Copy link

shacker commented Jan 20, 2014

Bingo! Adding lookup_field = 'username' to the Meta class of the UserSerializer class fixed the issue. Thanks much.

As to providing more assistance to new users, here, I would suggest:

  • Make the sample app and tutorial include a working implementation of just this scenario, so we have examples of both the default/pk lookup and custom lookups.
  • Be explicit. The doc you linked to, 'How hyperlinked views are determined', does not say explicitly, "Note that non-default/pk lookups must be specified identically in both the Serializer class and the ViewSet class." (Maybe it implies but I'm too dense to decipher it).
  • Add a similar note to the exception string. The current exception string,

"Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field."

unfortunately, doesn't quite get the developer to the right solution.

Thanks again.

@tomchristie
Copy link
Member

Thanks @shacker - great points.
I'll leave them against this ticket at the moment - the DjangoCon Weekend Cardiff sprints are a fortnight away - might make for a nice little set of tasks for a first time commiter to address.

@marcaurele
Copy link

Hi!

I have a similar issue with the same error message, trying to do the manul ViewSet on the User class, but I cannot get it working. I know that there is a shortcut for the user to just extend ModelViewSet, but I want to do the work manually (to understand). Could you tell me why I got the Could not resolve URL for hyperlinked relationship using view name "user-detail" ? I tried the lookup_field with 'pk' but it didn't work
Dpaste: https://dpaste.de/UJ19

I would really appreciate some help.

@scotartt
Copy link

Hi, I've got this issue or a very similar one also. I'm new to both django and django-rest. I added the lookup_field value to the user's 'Meta' class as @shacker says above. I still get this error

Could not resolve URL for hyperlinked relationship using view name "permission-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.

It's entirely mysterious what's going on here to me. you can see the code at https://github.com/scotartt/commentarius/tree/master/decommentariis

@ghost
Copy link

ghost commented Feb 19, 2014

Just wanted to chime in, I'm a new user, was running through the steps of the Example here:
http://www.django-rest-framework.org/

Though the above discussion temporarily solves the issue -- I just logged out of the admin area and tried again -- it does not make any sense to me as I'm new to the Django REST framework and fairly new to Django. Given this is going to be most newbie's immediate experience with the framework, and not everyone is going to find this discussion by Googling (I frequently assume when things go wrong that I'm doing it wrong, so might not search for the solution right away), it would seem wise to update at the very least the instructions to new users with a workaround in plain English, or remove the broken example.

I appreciate this discussion, glad I was able to workaround. Now on to the Quickstart and Tutorial... :)

@bekhzod91
Copy link

http://www.django-rest-framework.org/#example

from django.contrib.auth.models import Permission

class PermissionViewSet(viewsets.ModelViewSet):
model = Permission

need to add additional

@brodney
Copy link

brodney commented Apr 9, 2014

I was also able to fix this issue temporarily by explicitly declaring the fields for the user serializer, excluding the permissions field, like this:

class UserSerializer(serializers.HyperlinkedModelSerializer):
  class Meta:
    model = User
    fields = ('username', 'etc',)

lookup_field did not work in my case.

@tomchristie
Copy link
Member

^ Is the change we oughta make to the example yes.

@brodney
Copy link

brodney commented Apr 24, 2014

Should the example page include a UserSerializer? I'll update the documentation but I'm not sure what direction you'd like to take.

@spitfireinno
Copy link

The comment from myrubapa worked for me. Apparently we just need a few more __SetViews to address additional models. Any field that appears as a REST hyperlink seems to need such a view.

@tomchristie
Copy link
Member

Yes, it needs too - not including an explicit UserSerializer doesn't work because it'll default to including a field that is a hyperlinked relationship to permissions, which aren't also included as a view.

@ssikiki
Copy link

ssikiki commented Jul 11, 2014

Hi, I have tried the Update example code #1550, but still got the error "Could not resolve URL for hyperlinked relationship using view name 'permission-detail'" when I login and access url "/user/", what's wrong?
My env:
Django==1.6.5
Markdown==2.4.1
django-filter==0.7
djangorestframework==2.3.14

@tomchristie tomchristie changed the title Getting started issues Getting started issues. Aug 18, 2014
@tomchristie
Copy link
Member

Closing this off given the 2.4.0 release. The deprecation of the .model attribute and corresponding change in the README and front page examples fixes the bug that would be seen there if the user had any existing groups.

@cabello
Copy link

cabello commented Feb 26, 2015

I just started integrating django-rest-framework with an existent project and I am facing similar issues when following the quick start guide, I've added views for Users, Permissions, Groups but I am not sure what other relationships need to be properly configured in order for the HyperlinkedModelSerializer to work. For now I just settled for ModelSerializer, even though there are no links, is a good way to get a fresh start. :)

@tristanbbq
Copy link

Hi guys, not sure if there is another issue for that, but I'm having similar issues as @cabello - As soon as I try change my serializer to extend HyperlinkedModelSerializer, I get the error :/

@mcastle
Copy link
Contributor

mcastle commented Aug 4, 2015

An easy fix:

from django.contrib.auth.models import User


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        exclude = ('user_permissions',)

@bcoover
Copy link

bcoover commented Aug 16, 2016

Ok, I have a full and valid fix, add the following

Serializers:

from django.contrib.auth.models import Permission
class PermissionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Permission

Views:

class PermissionViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows permissions to be viewed or edited.
    """
    queryset = Permission.objects.all()
    serializer_class = serializers.PermissionSerializer

Urls:

router.register(r'permission', PermissionViewSet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests