A
A
Alexander Vinogradov2019-02-13 19:27:16
Django
Alexander Vinogradov, 2019-02-13 19:27:16

How to get get_absolute_url of the same model but from different url names?

How to get get_absolute_url of the same model but from different url names?
I have a StandardModel that contains cabinets.
These cabinets are categorized. Categories are written in the field with a choice

type_case = models.CharField(max_length=10, choices=TYPE_CASE, default='case', verbose_name='Тип корпуса')

Addresses (URL`s) are divided just into these categories, but all data is taken from this one model.
path('base/<b>case</b>/<int:pk>/', views.BasicItemView.as_view(), {'filter': 'case'}, name='<b>basiccaseitem</b>'),
    path('base/<b>built-in</b>/<int:pk>/', views.BasicItemView.as_view(), {'filter': 'built-in'}, name='<b>basicbuiltinitem</b>'),

To get the URL from the model, we write:
def get_absolute_url(self):
    return reverse('basiccaseitem', args=[str(self.pk)])

But here the problem is that we can only specify one name basiccaseitem . What if you need to return the URL for basicbuiltinitem as well?
spoiler

Модель
class StandardModel(models.Model):
    'Базовые шкафы'
    
    title = models.CharField(max_length=200, verbose_name='Название')
    article = models.CharField(max_length=10, unique=True, verbose_name='Артикул')
    qdoors = models.SmallIntegerField(choices=QUANTITY_DOORS, default=2, verbose_name='Количество дверей')
    type_case = models.CharField(max_length=10, choices=TYPE_CASE, default='case', verbose_name='Тип корпуса')
    .....

    def get_absolute_url(self):
        return reverse('basiccaseitem', args=[str(self.pk)])

URL.py
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('base/case/', views.BasicView.as_view(), {'filter': 'case'}, name='basiccase'),
    path('base/case/<int:pk>/', views.BasicItemView.as_view(), {'filter': 'case'}, name='basiccaseitem'),
    path('base/built-in/', views.BasicView.as_view(), {'filter': 'built-in'}, name='basicbuiltin'),
    path('base/built-in/<int:pk>/', views.BasicItemView.as_view(), {'filter': 'built-in'}, name='basicbuiltinitem'),
    path('base/doors/', views.DoorsView.as_view(), name='doors'),
    path('base/doors/<int:pk>/', views.DoorsItemView.as_view(), name='doorsitem'),
    path('works/', views.WorksView.as_view(), name='works'),
    path('works/<int:pk>/', views.WorksItemView.as_view(), name='worksid'),
]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question