I want to hide the item entries that are 0 or doesn't have any in stock, my current code for the item borrowing process is. The class Item is where I store the stock in the warehouse and the Activity is where i store the borrowed orders.
Models.py
class Item(models.Model):
ItemName = models.CharField(max_length=255, blank=True, null=True)
Quantity = models.IntegerField(null=True, default=1,
validators=[
MaxValueValidator(100),
MinValueValidator(0)
])
ModelNum = models.CharField(max_length=255, blank=True, null=True)
Category = models.ForeignKey(Category,on_delete=models.CASCADE, null=True)
date_created = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
is_draft = models.BooleanField(default=True)
reorder_level = models.IntegerField(blank=True, default=10,
validators=[
MaxValueValidator(100),
MinValueValidator(1)
])
class Meta:
verbose_name_plural = 'Item'
def __str__(self):
return f'{self.ItemName}'
class Activity(models.Model):
Item = models.ForeignKey(Item, on_delete=models.CASCADE, null=True)
staff = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True)
project_site = models.ForeignKey(Projects, on_delete=models.CASCADE, null=True)
Quantity = models.PositiveIntegerField(null=True, default=1,
validators=[
MaxValueValidator(100),
MinValueValidator(1)
])
date_created = models.DateTimeField(auto_now_add=True)
is_draft = models.BooleanField(default=True)
request_status = models.IntegerField(default=0,
validators=[
MaxValueValidator(3)
])
return_status = models.IntegerField(default=0,
validators=[
MaxValueValidator(3)
])
note = models.TextField(max_length=255, null=True)
class Meta:
verbose_name_plural = 'Item Request'
def __str__(self):
return f'{self.Item}'
Forms.py
class ActivityForm(forms.ModelForm):
class Meta:
model = Activity
fields = ['Item', 'Quantity', 'project_site']
CodePudding user response:
You can limit choices to Items that have a quantity that is at least one with the limit_choices_to=… parameter [Django-doc]:
class Activity(models.Model):
item = models.ForeignKey(
Item,
on_delete=models.CASCADE,
null=True,
limit_choices_to={'Quantity__gt': 0}
)
# …
Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be:
iteminstead of.Item
