-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Resolving APIs URL to different namespaces #3816
Conversation
Open question, would it be worth to alter the tests so they include one model in common and make sure it's resolved correctly for each namespace ? |
I wonder if some of the code can't be factorized with Django's. |
continue | ||
# Raise exception if everything else fails | ||
if not url: | ||
raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd really like to see an explicit raise here.
This syntax will work as long as no other exception is being thrown - no matter whether they are caught or not.
Given the code quantity between the initial except and this line it's subject to caution imho.
OK, with the restriction on the raise statement it looks good to me. |
Use case that may lead to raising the wrong exception:
Guess what raise will raise when url is None ? Is that use case possible ? NB: >>> try:
... 0/0
... except Exception:
... try:
... 'a' + 1
... except TypeError:
... pass
... raise
...
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> |
My super quick review --> I can see a lot of code, and not much explanation. What case exactly is being resolved here? If there's any other kind of workaround or the case isn't 100% clear cut then I'm unlikely to be in favor of this change. |
The point I don't get here is why it does try every namespace if it can't resolve without namespace or with the current namespace. |
For example, in a case when a model from an application under a namespace references another application model under a different namespace. |
I don't see how #4219 justifies closing this. This has nothing to do with versioning. If a namespace is in use, it would be nice for HyperlinkedModelSerializers to continue to function, at least where the linked detail route is in the same namespace as the current request. I'm not sure what the right thing to do is when it's in a different namespace - it's conceivable that the current app would know about a subtree of the overall namespace but not the whole thing. e.g. app with routes under "foo:api:firstapp" knows about the "api:firstapp" portion of the namespace, and that the model it's after is in "api:otherapp", but not that that is also now under the top-level "foo" namespace. Seems to me that it would be desirable to infer a prefix based on what is expected and experienced (expect to be in "api:firstapp" but discover that we're in "foo:api:firstapp", so infer "foo" prefix), and then use that to help find other known routes. |
In my use-case, the current version doesn't solve the problem. I still have to deal with models referencing each others through differents namespaces myself. Fortunatly, @xordoquy gives me the evidence that my previous solution have some caveats and can lead to undesired behaviour. Since, I changed the whole implementation of
|
For the moment I'm working around this with:
i.e. the serializer now knows which namespace it's in, and can pass this through to the fields. If a field is referring to an object in a different namespace, I have to specify that on the field. |
Oh, I've added get_extra_kwargs as below, to allow setting those prefixes from the serializer's Meta class too:
|
Can't we enforce explicitly namespaced view names in this case rather than magically inferring them? |
It depends how you want use your viewsets. In my case, we have a framework above our project in order to dynamically generate APIs from models with all filters, orders, related models and prefetchs... so having to deal with namespaces could lead to a lot of redundant work. Nevertheless, I understand your point of view: explicit is better than implicit. But I guess through this issue I'm not alone to require that kind of feature. :) |
You can't be explicit when you don't know the full namespace a pluggable app might be included under. |
An enough tested revision in order to allow resolving APIs URL using APIs in different namespaces.
Used as a patch in production environment since about one year without issue.
See #1495