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

Implement get_absolute_url() on BaseModel #2901

Closed
Tracked by #1574
jathanism opened this issue Nov 22, 2022 · 3 comments · Fixed by #3632
Closed
Tracked by #1574

Implement get_absolute_url() on BaseModel #2901

jathanism opened this issue Nov 22, 2022 · 3 comments · Fixed by #3632
Assignees
Labels
type: housekeeping Changes to the application which do not directly impact the end user
Milestone

Comments

@jathanism
Copy link
Contributor

jathanism commented Nov 22, 2022

Proposed Changes

Base models should always provide get_absolute_url() so that each model doesn’t have to hard-code this as well.

e.g. This should suffice:

class BaseModel(models.Model):
    def get_absolute_url(self):
        return reverse(get_route_for_model(self), args=[self.pk])

Justification

Since we’re aligning around pk for all objects, then this is even easier in that we know the app_label and model_name for the model, so we can automatically have something like above. We could just as easily align around the natural_key_slug (see: #2900) as well (e.g. args=self.natural_key_slug should we so desire).

More importantly, this is another boiler plate reduction. The majority of model objects are using {app_label}.{model} as their detail route name, and pk as the unique identifier in detail routes, so this would eliminate most places where get_absolute_url() is required to be defined in the common case.

@jathanism jathanism added the type: housekeeping Changes to the application which do not directly impact the end user label Nov 22, 2022
@jathanism jathanism added this to the v2.0.0 milestone Nov 22, 2022
@glennmatthews glennmatthews self-assigned this Feb 23, 2023
@glennmatthews
Copy link
Contributor

Models that currently do not have a detail view at all, and hence no immediately appropriate get_absolute_url implementation:

  • RelationshipAssociation
  • All ComponentTemplateModel subclasses (RearPortTemplate, etc.)

@bryanculver
Copy link
Member

Models that currently do not have a detail view at all, and hence no immediately appropriate get_absolute_url implementation:

  • RelationshipAssociation
  • All ComponentTemplateModel subclasses (RearPortTemplate, etc.)

Can we implement this in BaseModel and just pass/throw exceptions for these two cases until we have detail views for these (or reverse to a more sane detail view to view these?).

@bryanculver bryanculver linked a pull request Apr 21, 2023 that will close this issue
6 tasks
@bryanculver
Copy link
Member

Closed with #3632

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: housekeeping Changes to the application which do not directly impact the end user
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants