Add part category tree traversal

- If a category doesn't have a default_location, look at the parent category
- And so on and so on
This commit is contained in:
Oliver Walters 2019-05-04 21:56:18 +10:00
parent 71972f4454
commit 67eda51cd2
2 changed files with 13 additions and 2 deletions

View File

@ -159,8 +159,16 @@ class Part(models.Model):
if self.default_location: if self.default_location:
return self.default_location return self.default_location
elif self.category: elif self.category:
return self.category.default_location # Traverse up the category tree until we find a default location
cat = self.category
while cat:
if cat.default_location:
return cat.default_location
else:
cat = cat.parent
# Default case - no default category found
return None return None
# Default supplier part # Default supplier part

View File

@ -8,8 +8,11 @@
{% if category %} {% if category %}
<h3>{{ category.name }}</h3> <h3>{{ category.name }}</h3>
<p>{{ category.description }}</p> <p>{{ category.description }}</p>
{% if category.default_location %}
<p>Default Location: <a href="{% url 'stock-location-detail' category.default-location.id }%">{{ category.default_location }}</a></p>
{% endif %}
{% else %} {% else %}
<h3>Parts</h3> <h3>Part Categories</h3>
{% endif %} {% endif %}
</div> </div>
<div class='col-sm-6'> <div class='col-sm-6'>