borrowed some code from django-inventory