I am stuck trying to work out how to create a Django REST Framework ViewSet.
The API calls I have inherited look like this:
/api/v1/user/<user_id>/like_count
/api/v1/user/<user_id>/friends/
/api/v1/user/login
/api/v1/user/logout/
In my base urls.py I have the following:
urlpatterns = patterns('',
url(r'^api/v1/', include('api.urls')),
url(r'^$', TemplateView.as_view(template_name='base.html'), name='home'),
url(r'^docs/', include('rest_framework_swagger.urls'))
)
I have an app called api. In the api urls.py I have:
from django.conf.urls import url, include
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
router.register(r'user', views.UserViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
In my api/views.py file I want to create a UserViewSet class that handles all the possible variants of the url calls.
First I can't work out if I should use:
class UserViewSet(viewsets.ModelViewSet):
or...
class UserViewSet(APIView):
If I understand it correctly I can cater for the
/api/v1/user/login
/api/v1/user/logout
calls using something like:
class UserViewSet(viewsets.APIView):
def login(self, request, format=None):
...
def logout(self,request, format=None):
But I can't work out how to cater for the other variants that have the <user-id> in the url.
Is there a recommended way to do this?
Some API calls have a trailing '/' and some don't. It is what I have been given (to fit in with an existing mobile app).
EDIT: By the way, I have done the DRF tutorial twice now and still can't see how to do this.
EDIT2: I am really struggling to understand the DRF documentation for this. Can anyone provide some example code that deals with my use case?
Using a ViewSet or Views will depend on what other actions you need for your user resource apart from those you have in your urls. ModelViewSet is built from mixins for listing, updating etc. You can combine those to achieve what you want or you can just go with a combination of views.
Of course you can combine a viewset with views if you have to.
For the /api/v1/user/<user_id>/friends/ endpoint you can look at DRF docs or at DRF Nested Routers.
The like_count can be implemented with a view that returns just that single value.
Auth is a different topic entirely --- you can look at this.
viewsets.py
class UserViewset(viewsets.ModelViewSet):
lookup_field = 'id'
serializer_class = UserSerializer
queryset = User.objects.all()
urls.py
from .viewsets import UserViewSet
user_list = UserViewSet.as_view({'get':'list'})
user_detail = UserViewSet.as_view({'get':'retrieve'})
urlpatterns= [
url(r'^user/(?P<id>\d+)/(?P<friends>[-\w\d]+)/$', user_detail, name='user-detail')
]
In the serializers.py adde the ParameterisedHyperlinkedIdentityField as a serializer.
serializers.py
class UserSerializer(serializers.HyperlinkedModelSerializer):
url = ParameterisedHyperlinkedIdentityField(view_name='user-detail', lookup_fields=(('id', 'id'), ('friends', 'friends')), read_only=True)
class Meta:
model = User
Related
This is my main project urls . Now I have an app which is routed by path('', include('mysite.urls')),. Now I have a folder named crmAPi for creating apis. This api folder is not an app though, just a folder. I route the urls by using path('api', include("mysite.crmapi.urls")),.
urlpatterns = [
path('', include('mysite.urls')),
path('admin/', admin.site.urls),
path('api', include("mysite.crmapi.urls")),
]
Now, this is my crmApi urls. The problem is I can access to first path, i can see airilnes list. No, matter what i do, i cannot acces flightdetails. What is the problem with the route?? help pleaseee?? even i create a different listview, again I cannot the route, only the arilines one, I can access.
urlpatterns = [
path('', views.AirlinesListAPIView.as_view(), name='api'),
path('api/flightdetails', views.FlightDetailsListAPIView.as_view(), name='flightdetails'),
]
This is my view.
class AirlinesListAPIView(ListAPIView):
# permission_classes = [AllowAny]
serializer_class = AirlinesListSerailizer
queryset = Airlines.objects.all()
permission_classes = (IsAdminUser,)
class FlightDetailsListAPIView(ListAPIView):
# permission_classes = [AllowAny]
serializer_class = FlightDetailsListSerailizer
queryset = FlightDetails.objects.all()
permission_classes = (IsAdminUser,)
Well, you included the api.urls under api/ path. And inside api.urls you also have api/flightdetails, which contains api.
My assumption is that you're trying to access http://127.0.0.1/api/flightdetails, but it doesn't actually exist, because FlightDetailsListAPIView is accessible under http://127.0.0.1/api/api/flightdetails.
If you want access it via http://127.0.0.1/api/flightdetails, you should rename the path to:
path('flightdetails/', views.FlightDetailsListAPIView.as_view(), name='flightdetails'),
I'm trying to create a link for another app in my serializer using the solution provided here:
https://stackoverflow.com/a/45850334/12177026
I'm trying to match the view's name but every way I try I get this error:
Reverse for 'KnownLocationView' not found. 'KnownLocationView' is not a valid view function or pattern name.
serializers:
class MissionSerializer(HyperlinkedModelSerializer):
gdt = ChoiceField(choices=lazy(get_locations, tuple)())
location = SerializerMethodField(label='Open Location')
def get_location(self, obj):
request = self.context.get('request')
return request.build_absolute_uri(reverse('KnownLocationView', kwargs={'Name': obj.gdt}))
class Meta:
model = Mission
fields = ('MissionName', 'UavLatitude', 'UavLongitude', 'UavElevation', 'Area',
'gdt', 'location')
KnownLoction/urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import KnownLocationView
app_name = 'KnownLocation'
router = DefaultRouter()
router.register(r'knownlocations', KnownLocationView)
urlpatterns = [
path('', include(router.urls)),
]
I tried replacing view_name with either of the following:
'knownlocations'
'KnownLocation:knownlocations'
'KnownLocation:KnownLocationView'
But get the same error
even tried to reorder the installed apps.
api/urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('', include('landingpage.urls')), # API Landing Page
path('', include('ThreeLocations.urls')), # Triangulation between two Known GDTs and uav location.
path('', include('SecondGDT.urls')), # Options For Second GDT Positioning.
path('', include('KnownLocation.urls', namespace='knownlocations')),
# Add Known Location To the map.
] + staticfiles_urlpatterns()
KnownLocation/views.py
from rest_framework.renderers import AdminRenderer
from rest_framework.viewsets import ModelViewSet
from .models import KnownLocation
from .serializers import KnownLocationSerializer
class KnownLocationView(ModelViewSet):
"""
List of Known Locations In which we can place one of our ground positions.
press create to add a new location.
"""
serializer_class = KnownLocationSerializer
queryset = KnownLocation.objects.all()
def get_serializer_context(self):
data = KnownLocationSerializer(self.queryset, many=True, context={'request': self.request})
return data
renderer_classes = [AdminRenderer]
I think you should try to change how you are calling the reverse.
You can find reference to it below
Note: If using namespacing with hyperlinked serializers you'll also need to ensure that any view_name parameters on the serializers correctly reflect the namespace. In the examples above you'd need to include a parameter such as view_name='app_name:user-detail' for serializer fields hyperlinked to the user detail view.
The automatic view_name generation uses a pattern like %(model_name)-detail. Unless your models names actually clash you may be better off not namespacing your Django REST Framework views when using hyperlinked serializers.
Try using this
reverse('KnownLocationView:mission-detail, kwargs={'Name': obj.gdt})
Or
reverse('mission-detail, kwargs={'Name': obj.gdt})
I'm using Django REST Framework for the first time on a project and I'm struggling with one particular part. The project is an issue tracker-type applicaton and it uses django_comments to allow for comments on issues. I'm now building an API on top of it to allow for creating a mobile app.
I've created the following serializers:
from django.contrib.auth.models import User
from todolist.models import Project, Issue
from rest_framework import serializers
from django_comments.models import Comment
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'email', 'first_name', 'last_name')
class ProjectSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Project
fields = ('name', 'description', 'owner')
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
class IssueSerializer(serializers.HyperlinkedModelSerializer):
comments = CommentSerializer(many=True)
class Meta:
model = Issue
And here's my project-wide urls.py, where I define the routes:
from django.conf.urls import patterns, include, url
from todolist.views import HomeTemplateView, UserViewSet, ProjectViewSet, IssueViewSet, CommentViewSet
from rest_framework import routers
from rest_framework.authtoken.views import obtain_auth_token
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'projects', ProjectViewSet)
router.register(r'issues', IssueViewSet)
router.register(r'comments', CommentViewSet)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'projectile.views.home', name='home'),
# url(r'^projectile/', include('projectile.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# Comments
(r'^comments/', include('django_comments.urls')),
# Login
url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
# Logout
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login', {'login_url': '/accounts/login/'}),
# To-do list
url(r'^projects/', include('todolist.urls')),
# Home page
url(r'^$', HomeTemplateView.as_view(
template_name="todolist/home.html",
)),
# Router URLs
url(r'^api/', include(router.urls)),
# REST framework auth URLs
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api-token-auth/', obtain_auth_token),
# API docs
url(r'^docs/', include('rest_framework_swagger.urls'))
)
I'm seeing the following error when I run a test which tries to get all the comments:
ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "contenttype-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
Also, when I fetch an issue, it doesn't include the comments.
Now, this error appears to me to be that when fetching a comment, it's unable to match up the comment to its parent. I've experienced similar problems when using Tastypie in the past, and I'm unsure how to resolve it with DRF.
In your Comment serializer, specify comment instances fields explicitly and exclude field contenttype.
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = ('field_1','field_2','field_3') #here goes your comment fields and dont include contenttype field
The problem is that DRF will try to apply hyperlinks to represent all relationships, and for contenttype field you don't have a view or a serializer and you dont need it either.
I have a few URLs that I want to exclude from my REST API documentation. I'm using Django REST Swagger and the only documentation I can find (https://github.com/marcgibbons/django-rest-swagger) doesn't really tell me much. There is the "exclude_namespaces" part of SWAGGER_SETTINGS in settings.py, but there is no real explanation or example of how to use this.
Simply put, I want to exclude any URLs from the docs that start with the following:
/api/jobs/status/
/api/jobs/parameters/
How could I go about doing this?
Thanks in advance for any help offered :P
the namespaces to exclude are the one defined in your urls.py.
So for example, in your case:
urls.py:
internal_apis = patterns('',
url(r'^/api/jobs/status/',...),
url(r'^/api/jobs/parameters/',...),
)
urlpatterns = urlpatterns + patterns('',
url(r'^', include(internal_apis, namespace="internal_apis")),
...
)
and in your settings.py:
SWAGGER_SETTINGS = {
"exclude_namespaces": ["internal_apis"], # List URL namespaces to ignore
}
This is well described in there
For all of those who found the above answer not helpful:
I guess that "exclude_namespaces" doesn't work anymore in new versions of django swagger. I had almost the same problem (I didnt't want to show my internal apis in documentation) and the above solution didn't work for me. I've been searching for like an hour for a solution and finally found something helpful.
There are some attributes that you can pass to SchemaGenerator. One of them is urlconf. You can set it to be "yourproject.api.urls" and it will get only urls defined there! Of course, you have to make sure that all the urls that you want to exclude from your api documentation are not included there.
I hope that at least one person found my answer helpful ;).
A problem comes when you want to have many urls.py included in your api documentation. I don't know what should be done then. If anyone comes up with an answer to this new problem - feel free to comment my answer. thanks!
With new version of django swagger, we don't need to create view to exclude some urls. Below code will disable test2 url.
from rest_framework_swagger.views import get_swagger_view
urlpatterns1 = [
url(r'^', include(router.urls)),
url(r'^test/', include('test.urls')),
url(r'^test1/', Test2.as_view()),
]
schema_view = get_swagger_view(title='API Documentation', patterns=urlpatterns1)
urlpatterns = urlpatterns1 + [
url(r'^docs/', schema_view),
url(r'^test2/', Test2.as_view()),
]
Ola's answer is correct. exclude_namespaces is no longer supported.
For finer control of the documentation, create your own schema view by using a function-based or class-based view. This can be useful if you want to produce documentation for specific URL patterns, or URL confs.
In your views.py, you can do the following:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework_swagger import renderers
class SwaggerSchemaView(APIView):
renderer_classes = [
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]
def get(self, request):
generator = SchemaGenerator(title='Your API Documentation', urlconf='your_app.urls')
schema = generator.get_schema(request=request)
return Response(schema)
The above will only render documentation for the URLs that are specified in the urlconf argument of the SchemaGenerator. Also, don't forget to set up your urls.py as well:
from django.conf.urls import url
from views import SwaggerSchemaView
urlpatterns = [
url(r'^api/v1/docs/$', SwaggerSchemaView.as_view(), name='docs'),
]
For the newest version of drf-swagger you can defile url patterns in the schema generator.
For example:
url_patterns = (
url(r'^api/v1/', include(router.urls, namespace='api')),
)
generator = schemas.SchemaGenerator(title='Core API', patterns=url_patterns)
A more flexible solution would be:
from django.contrib import admin
from django.urls import include, path
from rest_framework_swagger.views import get_swagger_view
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('user.urls', namespace="user")),
path('locations/', include('location.urls')),
path('departments/', include('department.urls', namespace="department")),
path('my_secret_api/', include('secret.urls', namespace="secret_api")),
]
to_exclude = ['secret_api',] # some more namespaces here
swagger_urls = [item for item in urlpatterns if hasattr(item,"namespace") and item.namespace not in to_exclude]
schema_view = get_swagger_view(title='Highky', patterns=swagger_urls)
urlpatterns += [
path('api/docs/', schema_view),
]
urlpatterns will have all five paths, but swagger_urls will have four paths excluding secret_api.
All of your URLs and includes will continue to work as they were, except we are now passing our modified urlpatterns that we want to show in the Swagger docs. The checks will also cover the include where you don't specify a namespace (like in our case, where the namespace is not defined in the location).
views.py
any view class
class ...ViewSet(viewsets.ModelViewSet):
queryset = ....objects.all().order_by('-id')
serializer_class = ...Serializer
http_method_names = ['get', 'post', 'patch', 'delete'] # add or exclude
any function-based view
#api_view(['get']) # target field
def function(request):
...
return Response(...)
I have been trying to get django-registration to use the view RegistrationFormUniqueEmail and following the solution from this django-registration question. I have set my urls.py to
from django.conf.urls import patterns, include, url
from registration.forms import RegistrationFormUniqueEmail
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^users/', include('registration.backends.default.urls')),
url(r'^users/register/$', 'registration.backends.default.views.RegistrationView',
{'form_class': RegistrationFormUniqueEmail,
'backend': 'registration.backends.default.DefaultBackend'},
name='registration_register'),
)
However, I can still create multiple accounts with the same email. What is the problem? Shouldn't django-registration be using the view that I specified? I am currently using django-registration 0.9b1.
The version of Django registration you are using has been rewritten to use class based views. This means a different approach is required in your urls.py.
First, You need to subclass the RegistrationView, and set the custom form class.
from registration.backends.default.views import RegistrationView
from registration.forms import RegistrationFormUniqueEmail
class RegistrationViewUniqueEmail(RegistrationView):
form_class = RegistrationFormUniqueEmail
Then, use your custom RegistrationViewUniqueEmail subclass in your urls. As with other class based views, you must call as_view().
url(r'^user/register/$', RegistrationViewUniqueEmail.as_view(),
name='registration_register'),
Make sure your customised registration_register view comes before you include the default registration urls, otherwise it won't be used.
The version 1.2 of django-registration-redux allows the unique email option with the following urls.py patterns:
url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationFormUniqueEmail), name='registration_register'),
url(r'^accounts/', include('registration.backends.default.urls')),
If you need to do something more, like a specific URL option, you can subclass the RegistrationView in your app views.py and RegistrationForm in your app forms.py