I'm a Django beginner and I'm trying to create a warehouse management system in Django. The system keeps track of what products enters the warehouse and therefore knows what is in the warehouse and at which location.
There are several users (order pickers) who are logged in simultaneously and who have to get the products from the warehouse.
My model looks like this:
class Product(models.Model):
location = models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(5000)])
already_picked = models.BooleanField(default=False, blank=True)
available_for_picking = models.DateField(blank=True, null=True)
def __str__(self):
return str(self.id)
The products that have to be picked from the warehouse on a certain day must meet a number of conditions.
For instance:
- available_for_picking = today or date in the past
AND
- aldready_picked = False
What I want to do:
Every user goes to the url: www.mydomain.com/get_order_location_to_pick and gets a location of a product that he has to pick up from the warehouse.
However, there are several products that meet the selection criteria and there are several users who use the software at the same time. So I want to avoid assigning the same product (and location) to more than 1 user. When the user has taken the product, boolean "already_picked" should be changed from False to True. If the user has not been able to retrieve the product assigned to him from the warehouse for whatever reason, the product must be assigned to another user when he/she goes to the same url www.mydomain.com/get_order_location_to_pick
What's a good way to do this?
In my view I currently have this:
@login_required(login_url='login')
def get_order_location(request):
products_to_pick = Product.objects.filter(already_picked=False, available_for_picking__lte= datetime.now())
first_product_to_pick = products_to_pick.first()
context = {
'first_product_to_pick' : first_product_to_pick
}
return render(request, 'show_the_order_to_pick.html', context)
This isn't really working. It just gives the same product from the QuerySet and assigns it to all users currently visiting the url www.mydomain.com/get_order_location_to_pick
I read something about the Django function select_for_update() but I don't get my head wrapped around what it does. Is this something I might use to "lock" the first product to the first user while is busy looking for the product in the warehouse?
In a way I'm trying to assign a single record from a queryset to a user while he is performing an action on it and only making this object available for other users again when the transaction wasn't successful.