Home > Net >  Open listView with foreignkey from another listview with Django
Open listView with foreignkey from another listview with Django

Time:11-23

I am working on a product overview page in Django. For this I have three models: category, brand, product. I have created a View with ListView of the category. I loop through them to display them. I then want to open another overview of all brands within that category. How do I do this?

Here are my models:

    class Category(models.Model):
        category_name = models.CharField(max_length=200)
        sub_category = models.CharField(max_length=200,blank=True,null=True)
        category_picture = models.ImageField(upload_to='category/', null=True, blank=True)
    
        def __str__(self):
            if self.sub_category is None:
                return self.category_name
            else:
                return f" {self.category_name} {self.sub_category}"
    
        class Meta:
            ordering = ['category_name']
    
    class Brand(models.Model):
        category = models.ForeignKey('Category', on_delete=models.SET_NULL,null=True,blank=True)
        brand_name = models.CharField(max_length=200)
        brand_owner = models.CharField(max_length=200)
        brand_story = models.TextField()
        brand_country = models.CharField(max_length=200)
    
        def __str__(self):
            return f"{self.brand_name}"
        
    
class Bottle(models.Model):
    category_name = models.ForeignKey('Category', on_delete=models.SET_NULL,null=True,blank=True)
    brand = models.ForeignKey('Brand', on_delete=models.CASCADE)
    bottle_name = models.CharField(max_length=255)
    bottle_info = models.TextField()
    bottle_tasting_notes = models.TextField()
    bottle_barcode = models.IntegerField()
    bottle_image = models.ImageField(upload_to='bottles/',null=True)

    def __str__(self):
        return f"{self.brand.brand_name} {self.bottle_name}"

How do I open a listview of all brands within a certain category from a link of the category listview? Thank you.

CodePudding user response:

First thing is to create another listview that displays all brands within a specific category. We do this by creating a new get_queryset() method.

views.py

class BrandListView(ListView):
    model = Brand
    def get_queryset(self):
        return Brand.objects.filter(category__category_name=self.kwargs['category'])

Next add a url to your URLS.py so it can be accessed. We're using category_name as part of the url so it's human readable

from .views import BrandListView

urlpatterns = [
    path('brands/<str:category>/', PublisherBookList.as_view()), name= "brand_list"
]

Then, as you loop through your categories in your template, create a link to the url

{% for category in categories %}
{{category.category_name}} : <a href="{% url 'brand_list' category.category_name %}">See brands in category</a>
{% endfor %}

This will work as long as your categories have fairly simple names. If not, you might want to use the ID in the URL instead, or add a slug field to the model and use that.

  • Related