SynchronousOnlyOperation when generating regex expression for urls from database entries #1989
-
I am facing an issue now that I'm trying to integrate channels into my Django application. On one of my urls, I generate a regex expression which have a dependency from a database transaction. class NewsTypeManager(Manager):
def names_regex(self):
names = self.get_queryset().values_list('name', flat=True)
regex = r'(?P<newstype>'
try:
for n in names:
regex += r'|{}'.format(n)
except ProgrammingError:
regex += r'|{}'.format('')
regex += r')'
return regex router = DefaultRouter()
router.register(r'news/' + NewsType.objects.names_regex(), NewsModelViewSet, basename='news') I'm configuring the router like this: application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(apps.news.routing.websocket_urlpatterns)
),
}
) The error I'm having considers the line
I get it that |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The usual approach would be to match any name ( |
Beta Was this translation helpful? Give feedback.
-
You can move calculating the regex somewhere (like an AppConfig.ready()) where it's done after django.setup() but not in an async request context. |
Beta Was this translation helpful? Give feedback.
The usual approach would be to match any name (
[a-z]+
, or whatever) and then do the lookup in the view, returning a 404, if the name didn't match (asdfggh
, say)