Hi!
I have a piece of code that works fine, but i think i’m not doing it in the “only good practices” way.
Here we go. (My questions are below the code)
My Models:
#models.py
class Brand(models.Model):
name = models.CharField(max_length=100)
class Product(models.Model):
name = models.CharField(max_length=100, unique=True)
brand = models.ForeignKey(Brand, related_name='product', on_delete=models.PROTECT)
def get_total_weight_in_warehouse(self):
...
return weight
My goal: to show all products (and their “in_warehouse_weight”) of a brand on brand_detail.html.
What I did:
# views.py
class BrandDetailView(DetailView):
model = Brand
context_object_name = 'brand'
# brand_detail.html
...
{% for product in brand.product.all %}
...
{{ product.get_total_weight_in_warehouse }}
{% endfor %}
...
This works perfectly, but:
- Is it a bad practice to use product.get_total_weight_in_warehouse on a template?
- Is it a bad practice to use brand.product.all on a template?
- And if these are bad practices, how should i do it?
Thanks!