Need some help to wrap my head around this.
Got a model of equipment inventory.
There is a view to bulk add inventory by techs, when techs use the equipment the InventoryCheckOut models will track each piece of equipment (warehouse → tech float → on account → return)
So what I need help is to return context with only the equipment on the logged in techs float and not on account and in case the tech added the same equipment more than once then return that serial number only once.
I can’t figure out a filter setup what would return only the equipment what’s on tech_float status, only
context["serials"] = Inventory.objects.filter(inventorycheckout__linked_user = self.request.user)
# returns tech float but also return the one whats already on account and return same serial multiple times
context["serials"] = Inventory.objects.filter(inventorycheckout__float_type = 2, inventorycheckout__linked_user = self.request.user)
# returns nothing :(
# .distinct() also not working, using MySQL so cant add parameter to it like in PostgreSQL
Returns all serial added or modifiled by user ::
But also returns the entire history of the equipment regardless if its on tech float or on-account or return status
FLOAT_TYPES = [
('1', 'Tcopr EMD Warehouse'),
('2', 'Tech float'),
('3', 'On account'),
('4', 'Return'),
('5', 'Missing')
]
Serial # |-| Equipment model |-| Status
2143011113125321 748 2
2143011113125321 748 2
2143011113125321 748 3
2143011113125321 748 4
2143011113125321 748 1
2143011113125321 748 2
2143011113125321 748 3
2143011113125321 748 4
2143011113125546 748 1
2143011113125546 748 2
2143011113125546 748 3
#models.py
class Inventory(models.Model):
equipment_serial_number = models.CharField(max_length=256, null= False)
equipment_type = models.CharField(max_length=30, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.equipment_serial_number
class InventoryCheckOut(models.Model):
FLOAT_TYPES = [
('1', 'Warehouse'),
('2', 'Tech float'),
('3', 'On account'),
('4', 'Return'),
('5', 'Missing')
]
linked_user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, default=None)
float_type = models.CharField(max_length=30,choices=FLOAT_TYPES, null= False, default=1)
linked_inventory = models.ForeignKey(Inventory, on_delete=models.DO_NOTHING, default=None, null=True)
created_at = CustomDateTimeField(auto_now_add=True)
#views.py
class TechInventoryViews(TemplateView):
template_name ='list_tech_inventory.html'
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(TechInventoryViews, self).dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["serials"] = Inventory.objects.all() # return all serial number (not what we want)
return context