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

Wrong detail url in polymorphic models #876

Open
2 tasks done
mmzeynalli opened this issue Jan 31, 2025 · 0 comments
Open
2 tasks done

Wrong detail url in polymorphic models #876

mmzeynalli opened this issue Jan 31, 2025 · 0 comments

Comments

@mmzeynalli
Copy link

mmzeynalli commented Jan 31, 2025

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

I have polymorphic models, and when I click "view" or "edit" its base url is changed, resulting in 404.

Steps to reproduce the bug

from sqlalchemy.orm import DeclarativeBase

class PinnedObject(DeclarativeBase):
    __tablename__ = 'core_pinnedobject'
    id: Mapped[intpk]
    pinnedable_type: Mapped[str] = mapped_column(String)
    pinnedable_id: Mapped[int]

    __mapper_args__ = {
        'polymorphic_identity': __tablename__,
        'polymorphic_on': pinnedable_type,
    }

class NewsPinned(PinnedObject):
    __mapper_args__ = {
        'polymorphic_identity': 'news_pinned',
    }


class VacancyPinned(PinnedObject):
    __mapper_args__ = {
        'polymorphic_identity': 'vacancy_pinned',
    }

and when I create admin class for PinnedObject:

class PinnedObjectAdmin(ModelView, model=PinnedObject):  # type: ignore[call-arg]
    def _build_url_for(self, name, request, obj):
        # Need this bcuz of polymorphism: object can be NewsPinned or VacancyPinned
        # resulting in different generated URLs
        return request.url_for(
            name,
            identity=slugify_class_name(PinnedObject.__name__),  # the original is `slugify_class_name(obj.__class__.__name__)`
            pk=get_object_identifier(obj),
        )

Expected behavior

In the list view, the base url is pinned-object/list. When I click to one item it should beCCC

Actual behavior

However, link changes to news-pinned/edit/{id} or vacancy-pinned/edit/{id} resulting to 404 page. I had to overwrite _build_url_for function, to force class name to PinnedObject. I believe, instead of obj.__class__.__name__ it should take model of the AdminView.

Debugging material

No response

Environment

  • OS: Windows
  • Python: 3.11
  • SQLAdmin: 0.20.1

Additional context

No response

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

1 participant