How to use UserViewSet in djoser 2.0.3? - django

I am new to Djoser and I am struggling to use the functions UserView, UserViewCreate, UserViewDelete to update,create and delete users.
The code I am using currently in url_patterns is:
from django.conf.urls import re_path
from djoser import views as djoser_views
urlpatterns = [
re_path(r'^user/view/$', djoser_views.UserView.as_view(), name='user-view'),
re_path(r'^user/delete/$', djoser_views.UserDeleteView.as_view(), name='user-delete'),
re_path(r'^user/create/$', djoser_views.UserCreateView.as_view(), name='user-create'),
]
When I use UserView, I am getting the error as :
AttributeError: module 'djoser.views' has no attribute 'UserView'
I read the djoser documentation and saw that:
UserCreateView, UserDeleteView, UserView, PasswordResetView,SetPasswordView, PasswordResetConfirmView, SetUsernameView, ActivationView, and
ResendActivationView
These functions have all been removed and replaced by appropriate sub-views within UserViewSet.
I searched but couldn't find any way to use UserViewSet. Is there any way to use UserViewSet in djoser 2.0.3?

you could try
urlpatterns = [
re_path(r'^user/view/$', djoser_views.UserViewSet.retrieve.as_view(), name='user-view'),
re_path(r'^user/delete/$', djoser_views.UserViewSet.destroy.as_view(), name='user-delete'),
re_path(r'^user/create/$', djoser_views.UserViewSet.create.as_view(), name='user-create'),
]
but I would recommend following there documentation and use the lib as documented.

Related

Setting up simple Django ViewSet APIs from multiple apps

I’m still learning django and assume this may be easy for some. I’m trying to figure out the best way of simply setting up the API URLs (and so that they all display in the api root and can actually be used in the project - in my case at /api/). I’m using django rest framework, and can’t seem to set up more than one API - it works for just one, but the problem occurs when trying to set up another.
So I have an app called pages and accounts (and core - the default where the main urls.py is). I’ve created another urls.py inside the pages app and another inside the accounts app.
accounts/urls.py:
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r"accounts", views.AccountsViewSet, basename="accounts")
urlpatterns = router.urls
pages/urls.py:
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r"pages", views.PagesViewSet, basename="pages")
urlpatterns = router.urls
And the core urls.py:
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from rest_framework.routers import DefaultRouter
router = routers.DefaultRouter()
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include("pages.urls")), # works but root only shows pages API
# path("api/", include("pages.urls", "accounts.urls")), # fails with error: “django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.”
# path("api/", include(router.urls)), # no error but root api is empty
]
I would assume, possibly incorrectly, that just router.urls should include all the apis when visiting the root. The root apis currently looks like this when using just include("pages.urls”):
{
"pages": "http://localhost:8000/api/pages/"
}
How can I get it to correctly show all apis? The only way I could do it was by putting router.register(r"accounts", views.AccountsViewSet, basename="accounts”) in the pages urls.py, which is very undesirable, especially as the project grows even further.
Thank you
Have you tried to use:
path("api/pages/", include("pages.urls")),
path("api/accounts/", include("accounts.urls")),
In your urls.py?
Possibly that would mean your routes would be:
{
"pages": "http://localhost:8000/api/pages/pages/"
"accounts": "http://localhost:8000/api/accounts/accounts/"
}
In that case you could try to use
router.register("", views.AccountsViewSet, basename="accounts")
in accounts/urls.py.
And similarly,
router.register("", views.AccountsViewSet, basename="pages")
in pages/urls.py.
That way, you might achieve to have routes like:
{
"pages": "http://localhost:8000/api/pages/"
"accounts": "http://localhost:8000/api/accounts/"
}
if that is what you want.

rest framework swagger 'AutoSchema' object has no attribute get_link

I am trying to browse the api documentation of rest framework swagger. but getting error AttributeError: 'AutoSchema' object has no attribute 'get_link
I have used django rest framework swagger. but not able to browse the documentation.
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API')
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('auth.urls')),
path('docs/', schema_view),
]
add below line in settings.py
REST_FRAMEWORK = {'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.coreapi.AutoSchema' }
djagno-rest-swagger is deprecated and suggests to use drf-yasg
use drf-yasg it is more useful than djagno-rest-swagger
When I add this to my seetings.py, works fine
REST_FRAMEWORK = {
...
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}

Replacing default forms in django-postman via urlpatterns and views

django-postman docs say that you can replace the default forms in views with this:
urlpatterns = patterns('postman.views',
# ...
url(r'^write/(?:(?P<recipients>[^/#]+)/)?$',
WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)),
)
But what is patterns? Where do I import that from, and where would this code go? In the project's urls.py?
My project level urls.py currently includes django-postman, as recommended in the docs, like this:
urlpatterns = [
...
url("r'messages/', include('postman.urls', namespace='postman'),
]
So the custom url pattern should be overwriting the default that will already be included in urls.py.
Yes, this is a code for urls.py. However, it's quite outdated.
The modern version would look like:
from django.urls import re_path
urlpatterns = [
re_path(r'^write/(?:(?P<recipients>[^/#]+)/)?$',
WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)),
]
Edit
I guess you're including postman urls in your root urls.py, then you can do something like this to overwrite one of them:
urlpatterns = [
...
re_path(r'^messages/write/(?:(?P<recipients>[^/#]+)/)?$',
WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)),
path('messages/', include('postman.urls')),
]

In Django 1.11 getting Circular Import Errors When including app URLconf files in project URLconf File

I'm new to Django, have been doing several tutorials to get really comfortable with the structuring, and am now running through the official tutorial.
I've created an polls App, which has a polls/views.py file as follows:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, World. You're at the polls index.")
I've also created an App URLconf file polls/urls.py with the following code:
from django.conf.urls import url
from . import views
url_patterns = [
url(r'^$', views.index, name='index'),
]
This is pretty much exactly as done in the Django tutorial.
My issue is when I'm specifying url routes in the main projectname/url.py file on a project level as such:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
When doing this, I get the following error:
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from 'ProjectFolder\\polls\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
This is how the official Django tutorial dictates it to be done. However, if I explicity import the polls/views.py file from the app, I can accomplish the task as follows:
from django.conf.urls import url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', views.index),
]
My immediate concern is the import of every app/urls file I ever create being necessitated by this approach, as well as the obvious divergence from the official Django instruction.
I hesitated to even ask this question because I feel that such a fundamental issue has bound to have an easy fix. Any help would be greatly appreciated.
To clarify, I can get around the error by explicitly importing the view files from apps. Whenever using the Django documentation-described approach of using the include() function I receive the error. I can appreciate the value of this function, and would like to know why is giving me the error described above.
Just writte urlpatterns = [ .. and not url_patterns in your poll.views.py.

Django url dispatcher uses strings to specify function, why?

The Django documentation shows examples like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
]
However, I have seen some code that looks like this:
from django.conf.urls import url
urlpatterns = [
url(r'^articles/2003/$', 'myapp.views.special_case_2003'),
]
Where special_case_2003 is the name of a function in myapp/views.py.
What is the difference between these two approaches?
urlpatterns = [
url(r'^articles/2003/$', 'myapp.views.special_case_2003'),
]
Code like this is out of date. Providing the view as a string like this is deprecated in Django 1.8, and does not work in Django 1.10+. In Django 1.10+, you must use the callable.