Skip to content
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

Managing the combination of NestedSimpleRouter.lookup and GenericViewset.lookup_field #147

Open
ghost opened this issue Jul 15, 2019 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 15, 2019

Having a bit of trouble with this but it could just be me: is it possible to just use only one of these fields to declare a lookup string? Currently we have a non-primary-key field that's used for lookup, but we end up with some weirdness.

Leaving the default lookup_field on our GenericViewset subclass causes any of our "lookup" text to have _pk appended to it, such as foobar_pk

If we update the lookup_field we can get foobar_foobar, but trying to set lookup='' for the router leaves us with _foobar

I see this behavior comes from the very beginning of NestedMixin.__init__... Basically I just want the lookup field to only be foobar with nothing else, is there a way to do that or am I missing something?

@dsalisbury
Copy link

This is an issue that I've faced too -- and I found a sneaky solution for it. In NestedMixin.__init__, I saw that it calls self.nest_prefix = something_somethingelse, so what if we just make that a property that doesn't do anything when assigned, and returns an empty string when retrieved? Well that works, but the __init__ method later validates that self.nest_prefix matches an acceptable regex; by making that blindly return True it all works -- although the return value isn't actually checked.

class NestedMixin(rest_framework_nested.routers.NestedMixin):
    @property
    def nest_prefix(self):
        return ""

    @nest_prefix.setter
    def nest_prefix(self, value):
        pass

    def check_valid_name(self, value):
        return True

class NestedSimpleRouter(NestedMixin, SimpleRouter):
    pass

Before:

Screen Shot 2019-07-26 at 3 46 22 pm

After:

Screen Shot 2019-07-26 at 3 45 48 pm

@mdefeche
Copy link

Any chance this would be integrated in the lib somehow ? Extra param to disable prefix or simply a rule that disable prefix when lookup param is empty ?

@alanjds
Copy link
Owner

alanjds commented Sep 22, 2022

Any chance this would be integrated in the lib somehow ? Extra param to disable prefix or simply a rule that disable prefix when lookup param is empty ?

I cannot say that I truly understand the implications of this change. Yet is very possible to be accepted, given that new automated tests do pass along the existing ones in a PR 👍 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants