2020-12-02 06:07

Django Channels Unresolved Import channels.generic.websockets

  • GraphQL AioWS version: Django 2.1.5
  • Python version: Python 3.6.6 :: Anaconda, Inc.
  • Operating System: macOS Mojave 10.14


I'm trying to set up GraphQL Subscriptions with Django Channels 2, but the code keeps on throwing this error: ModuleNotFoundError: No module named 'channels.generic.websockets'

What I Did

I checked the Django Channels docs and also the source code of graphql_ws and figured a typo: In the first line of django_channels.py, instead of

from channels.generic.websockets import JsonWebsocketConsumer

should be

from channels.generic.websocket import JsonWebsocketConsumer

reference: https://channels.readthedocs.io/en/latest/topics/consumers.html#jsonwebsocketconsumer

python manage.py runserver


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_39786850 weixin_39786850 5月前

    Hey I just followed you can saw your repository of a messenger in Django / react. Great, before switching to Vue I tried to do the same thing with that stack, before I find Vue more lovable.

    Since I feel that we are on the same page, would you mind if I ask you to share your experience in implementing GraphQL in the backend. Opinions from the perspective from a Django background are scarce these days.

    I just researched Prisma a bit and found it super cool. It has more stars than Apollo yet fewer downloads.

    To give up on on Django-graphene, I have looked at so many unfamiliar things, including:

    • Apollo server (Most used overall, but I found it not abstracted)
    • Strapi (seems more abstracted, though have to use Koa2)
    • Prisma (seems abstracted to me as well)
    • others I cannot recall...

    Surely, Django has super high abstraction level and heavy reliance on opinions and ready recipes, which suits my needs very well. The generic class-based views really made development speed on steroids - last time, I made a small enterprise admin site in 3 days.

    So as switching to Node, since I am currently just a one-man army, I really would prefer the possibility to write less code and get more magic.

    The ease of frontend coding is also important. Surely, migrating from Graphene to Node can be done in no time, but then it's still a huge pain having to write code in the frontend to consume these APIs. Basic CRUD / Filter / Pagination operations should be just boilerplate code. When I looked at Strapi, it seems that they have a GUI for Model development and some kind of CodeGen utility that generates the logic part of the frontend code (not the presentation) and I'm like WOW.

    Please share your thought on which framework have more magic :) Thanks

    点赞 评论 复制链接分享
  • weixin_39786850 weixin_39786850 5月前

    Thanks for your generous sharing of opinion! I will research some more soon, but just to quickly recap what I have learned today, Prisma seems really a magical tool. What I am currently doing is making a code generator which takes a Django schema, and generate Grapheme schema, test data, Apollo client code, mutatuions, a VueX store (similar to Redux) and a Vue CRUD Frontend... I think although this is hardly proper programming, it help me achieve in a relatively short time.

    So then I thought about in Create react app or React native, and wondering if my codegen approach is quite similar to how these frameworks abstract away the low-level codes

    Yes, so once again my goal is to be as high-level as possible. Haha

    点赞 评论 复制链接分享
  • weixin_39526546 weixin_39526546 5月前

    Nice. Good luck with your project! Ask me anything if you need help!

    点赞 评论 复制链接分享
  • weixin_39526546 weixin_39526546 5月前

    I switched to using https://github.com/datadvance/DjangoChannelsGraphqlWs because graphql-ws wasn't working for me. Note that DjangoChannelsGraphqlWs doesn't work for python 3.6 so what I did was I modified the source code.

    点赞 评论 复制链接分享
  • weixin_39930144 weixin_39930144 5月前

    Here is the link to the related discussion about the future and maintenance plan of this project: graphql-python/graphene/issues/884

    点赞 评论 复制链接分享
  • weixin_39786850 weixin_39786850 5月前

    I am facing the same issue. So did you mean you fixed the typo and got it to work, or you switched to DjangoChannelsGraphqlWs and then made some changes to make it work...

    I have hit so many barriers implementing GraphQL + Django + VueJS, that right now I am even thinking of just move on to Node/Express... The stack would have many more readily available recipes

    点赞 评论 复制链接分享
  • weixin_39526546 weixin_39526546 5月前

    I tried using graphql-ws but it didn't work, so I switched to using DjangoChannelsGraphqlWs. However DjangoChannelsGraphqlWs is written strictly for python3.7+, so I modified the source code to using python3.6. I'm currently using node.js prisma and reactjs for my project since django took too long to implement.

    点赞 评论 复制链接分享
  • weixin_39786850 weixin_39786850 5月前

    Well at least I got that part right, switching to Node....


    点赞 评论 复制链接分享