Search code examples
djangodjango-modelsdjango-viewsdjango-templatesdjango-filter

How can i get all the product categories a suppliers product belongs


I am working a Supplier Management System. I need to make a particular type of query which I am having issue implementing. I have the user models, and then the user_type which is of two types the suppliers and the admin. Of Course, the filter I need to implement is based of the supplier because only the supplier is able to create product in which they have to specify what categories as well.

My Problem: How can I get all categories a supplier products belongs to.

My Problem Edit: How can get each suppliers products and pass into the templates on the <td> tag

models.py

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
        
    def get_email(self):
        return self.email

class user_type(models.Model):
    is_admin = models.BooleanField(default=False)
    is_supplier = models.BooleanField(default=False)
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__(self):
        if self.is_supplier == True:
            return User.get_email(self.user) + " - is_supplier"
        else:
            return User.get_email(self.user) + " - is_admin"

    @property
    def get_categories(self):
        return Category.objects.filter(product__user=self.id).distinct()

class Category(models.Model):
    name = models.CharField(max_length=256)

    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=36)
    price = models.PositiveIntegerField()    
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

views.py

def Viewsupplier(request):
    title = "All Suppliers"
    suppliers = User.objects.filter(user_type__is_supplier=True)
    categories = Category.objects.filter(product__user='2').distinct()
    context = {"suppliers":suppliers, "title":title, "categories":categories}
    return render(request, 'core/view-suppliers.html', context)

view-suppliers.html

<table class="table table-borderless table-data3">
<thead>
<tr>
    <th>No</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>Category(s)</th>
    <th>Country</th>
</tr>
</thead>
<tbody>
{% for supplier in suppliers %}
<tr>
    <td>{{forloop.counter}}</td>
    <td>{{supplier.email}}</td>
    <td>{{supplier.telephone}}</td>
    <td>{{supplier.get_categories}}</td>
    <td>{{supplier.country}}</td>
</tr>
{% empty %}
    <tr><td class="text-center p-5" colspan="7"><h4>No supplier available</h4></td></tr>
{% endfor %}
</tbody>
</table>

Solution

  • You can filter with:

    Category.objects.filter(product__user=myuser).distinct()

    where myuser is the user you want to filter on.

    The .distinct(…) [Django-doc] will prevent returning the same Category that many times as there are Products for that user.