28 – HOW TO SETUP ALERT WHEN STOCK QUANTITY REACH A CERTAIN LEVEL

Spread the love

1. Make a form for updating the reorder level

class ReorderLevelForm(forms.ModelForm):
	class Meta:
		model = Stock
		fields = ['reorder_level']

2. Make a view for reorder level

def reorder_level(request, pk):
	queryset = Stock.objects.get(id=pk)
	form = ReorderLevelForm(request.POST or None, instance=queryset)
	if form.is_valid():
		instance = form.save(commit=False)
		instance.save()
		messages.success(request, "Reorder level for " + str(instance.item_name) + " is updated to " + str(instance.reorder_level))

		return redirect("/list_items")
	context = {
			"instance": queryset,
			"form": form,
		}
	return render(request, "add_items.html", context)

3. Make a url for editing the reorder level

path('reorder_level/<str:pk>/', views.reorder_level, name="reorder_level"),

4. Make a template tag condition with a div rapping the quantity field that will apply a style to the field when the reorder level is equal to or less than the quantity

<td>
  {% if instance.quantity <= instance.reorder_level %}
  <div style="background-color: orange;">{{instance.quantity}}</div>
  {% else %}{{instance.quantity}}
  {% endif %}
</td>

5. Add a column for reorder level and make is clickable to update the reorder level

<td><a href="{% url 'reorder_level' instance.id %}">{{instance.reorder_level}}</a></td>

Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

7 Comments

Leave a Reply to Humberto Cancel reply

Your email address will not be published. Required fields are marked *