objects.filter(help please)

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

I’m a little confused by your description relative to the models you have defined.

Since you have a serial_number within the Inventory, I get the impression that each individual piece of equipment would have one entry in this table. (e.g. Have 10 identical laptops? There would be 10 rows in the table.)

However, your InventoryCheckOut model is defined as a “Many-to-One” relationship through a ForeignKey. That means that each piece of equipment may have many CheckOut rows associated with it.

Is this what you’re intending to do?

If not, then you need to re-think how you’ve designed your models and what you’re actually trying to represent and track.

I agree, the model probably incorrect, its not in the production so I can change it to whatever would work.
This is what I want to do.

  1. Equipment arrives warehouse from vendor.
  2. Serial added to DB (serial entered to Inventory)
  3. InventoryCheckOut entry also created and FLOAT_TYPES=1
  4. Tech take equipment out of warehouse InventoryCheckOut FLOAT_TYPES=2
  5. Tech install equipment InventoryCheckOut FLOAT_TYPES=3
  6. Customer return equipment InventoryCheckOut FLOAT_TYPES=4
  7. Staff refurb equipment InventoryCheckOut FLOAT_TYPES=1
  8. keep changing on the same loop unless…
  9. Equipment lost or damaged InventoryCheckOut FLOAT_TYPES=5

In case vendor asking for equipment, a report can be generated on the history of the equipment, tech lost equipment than we got a timestamp when it was assigned to tech (and who got it)
I could probably change the add_to_tech_float to check if is already on tech float and dont create new entry to InventoryCheckOut FLOAT_TYPES=2
The reason got it that way currently as the company do monthly audit on tech_float by rescanning all equipment (this is done currently in excel ::slight_smile:

Any recommendation on a model ?

So what it sounds like to me is that you want the status to be a field in the inventory, with an InventoryHistory model associated with it.

The simple, direct approach is to add a row to the InventoryHistory model every time the Inventory model is changed. (Note: This approach does have some holes. There are ways of updating the table that wouldn’t result in the history being updated. To provide that level of control, you’d need to add a database trigger to insert data into the history table for every change to the inventory table - Django can’t provide that degree of assurance to you.)