views:

53

answers:

2
from django.db import models
from django.contrib.auth.models import User

class Product(models.Model):
name = models.CharField(max_length = 127)
description = models.TextField()
code = models.CharField(max_length = 30)
lot_no = models.CharField(max_length = 30)
inventory = models.IntegerField()
commited = models.IntegerField()
available = models.IntegerField()
reorder = models.IntegerField()
created_date = models.DateField(auto_now_add = True)
comment_user = models.ForeignKey(User, null=True)
comment_txt = models.TextField()

def __unicode__(self):
    return self.code + " - " + self.name + " - " + self.lot_no + " - " + str(self.created_date)

I was wondering how I would be able to have a math function such that available = inventory - comitted such that when someone enters in values for inventory and comitted it will subtract the 2 and place that value into available. Than if reorder being inventory - available is <= available it will flag as red textbox or something of the sort.

Thank you

A: 

I don't understand your last sentence, but for the rest of it, showing a calculated field in the admin list_display is easy - just create a method on the model or the admin. In your case, the easiest thing is to drop the existing 'available' field and use a model method marked as a property.

@property
def available(self):
    return self.inventory - self.committed
Daniel Roseman
I don't understand how to do this can you go into more detail?
Jon
A: 

for example if reorder is <= available than the reorder integerfield would come up as a red flag saying you need to reorder or something of the sort.

Jon