1

In my app, there's a list of categories and subcategories with a ForeignKey relationship. Say, there're:

  • Subcategory1 related to Category1
  • Subcategory2 related to Category2

I expect to get the following subcategory urls:

These urls work fine. However, django also generates these urls that I don't need:

Why do they appear in my app? How do I get rid of them? Thanks in advance!

models.py:

class Category(models.Model):
    categoryslug = models.SlugField(max_length=200, default="",unique=True)

    def get_absolute_url(self):
        return reverse("showrooms_by_category",kwargs={'categoryslug': str(self.categoryslug)})

class Subcategory(models.Model):
    subcategoryslug = models.SlugField(max_length=200, default="",unique=True)
    category = models.ForeignKey('Category', related_name='subcategories', 
    null=True, blank=True, on_delete = models.CASCADE)

    def get_absolute_url(self):
        return reverse("showrooms_by_subcategory",
        kwargs={'categoryslug': str(self.category.categoryslug), 'subcategoryslug': str(self.subcategoryslug)})

views.py:

class ShowroomCategoryView(DetailView):
    model = Category
    context_object_name = 'showrooms_by_category'
    template_name = "website/category.html"
    slug_field = 'categoryslug'
    slug_url_kwarg = 'categoryslug'


class ShowroomSubcategoryView(DetailView):
    model = Subcategory
    context_object_name = 'showrooms_by_subcategory'
    template_name = "website/subcategory.html"
    slug_field = 'subcategoryslug'
    slug_url_kwarg = 'subcategoryslug'

urls.py:

urlpatterns = [
    path('<slug:categoryslug>/<slug:subcategoryslug>/', views.ShowroomSubcategoryView.as_view(), name='showrooms_by_subcategory'),
    path('<slug:categoryslug>/', views.ShowroomCategoryView.as_view(), name='showrooms_by_category'),
]
tikhul
  • 11
  • 3
  • What do you mean by "generate"? Django tries to resolve paths: If it resolves a path of invalid cat-subcat combo, just raise a `Http404` in the view. – user2390182 May 04 '20 at 11:11
  • Thanks for your suggestion, I will research on this. If you could give me a little hint, I'd appreciate it a lot. Which method should I use? Could you show me an example? – tikhul May 04 '20 at 11:43

1 Answers1

0

I think the reason for this is foreign_key. So, I think as you can use one to one field to get the target, like:

subcategoryslug = models.SlugField(max_length=200, default="",unique=True)
category = models.OneToOneField('Category', related_name='subcategories',null=True, blank=True, on_delete = models.CASCADE)

*Note:- Please understand the logic too behind it. For that, do research more.

Biplove Lamichhane
  • 3,995
  • 4
  • 14
  • 30
  • While researching my problem, I found a very got explaination of the relationships: https://stackoverflow.com/questions/25386119/whats-the-difference-between-a-onetoone-manytomany-and-a-foreignkey-field-in-d In my case, many Subcategories could be related to one Category. So, I believe OneToOne won't work for me, right? – tikhul May 04 '20 at 11:33