Hi I’m writing an hardware inventory application, and I’d like to log each time a new part get’s entered in, along with any future status changes/updates.
models
class Part(models.Model):
type = models.ForeignKey(PartType, blank=False)
bar_code = models.CharField(max_length=50, blank=False, unique=True)
serial_number = models.CharField(max_length=50, blank=False)
status = models.ForeignKey(Status, blank=False)
class PartLog(models.Model):
part = models.ForeignKey(Part, blank=False)
time_stamp = models.DateTimeField(blank=False, auto_now_add=True)
old_status = models.ForeignKey(Status, related_name='old_status_related', blank=False)
new_status = models.ForeignKey(Status, related_name='new_status_related', blank=False)
class Status(models.Model):
current_status = (
("EN", "Entered Database"),
("CO", "Checked out"),
("CI", "Checked in"),
("RM", "Returned for RMA"),
("IU", "Currently in use"),
)
status = models.CharField(max_length=2, choices=current_status)
def __unicode__(self):
return unicode(self.status)
signals
@receiver(post_save, sender=Part)
def add_to_partLog(sender, instance, signal, created, *args, **kwargs):
if created:
print "Since a new entry has been created, setting old and new status for partlog entry!"
# Automatically changing status from Entered state to Checked In state
# Part Table
instance.status=Status(3)
instance.save()
# Setting Old and New status for the first (new) partLog entry for the new added part
# Part Log table
oldStatus = Status(1)
newStatus = Status(3)
partobj = Part.objects.get(id=instance.pk)
PartLog.objects.create(part=partobj,old_status=oldStatus, new_status=newStatus)
else:
print "Entry already exists!"
# Retreiving the old status of the Part() record
oldStatus = ???
newStatus = instance.status
partobj = Part.objects.get(id=instance.pk)
PartLog.objects.create(part=partobj,old_status=oldStatus,new_status=newStatus)
views
def check_in_part(request):
err_list=[]
c = {}
c.update(csrf(request))
if request.method == 'POST':
form = PartForm(request.POST)
print "form object is created."
if form.is_valid():
form.save()
return http.HttpResponseRedirect('/current_count/')
else:
form = PartForm(initial={'status':1L})
return render(request,'add_part.html',{
'title':'Add Item',
'form':form
})
# Checking out a part
def check_out_part(request):
errlst = []
c = {}
c.update(csrf(request))
# ADD check against DB with the appropriate status "CO" or 3
if request.method == 'POST':
form = ModifyPartForm(request.POST)
if form.is_valid():
bar_code_form = form.cleaned_data['bar_code']
try:
bar_code_model= Part.objects.get(bar_code=bar_code_form)
except Part.DoesNotExist:
#FIXME: need to get errlst to user
errlst.append("Part with bar_code %s does not exist." % bar_code_form)
else:
#bar_code_model.check_out()
bar_code_model.status_id=2L
bar_code_model.save()
return http.HttpResponseRedirect('/current_count/')
else:
form = ModifyPartForm()
# Adding default status to Check Out or "CO"
return render(request, 'remove_part.html',{
'title':'Remove Item',
'form': form,
'errors': errlst,
})
The problem lies within the else clause in the signals. The first part of the if works just fine. *I’m not sure how to access the “status” before it was saved and use it on the post save. * Right now in one view, when I check_in a new part I’m making the initial status “EN”, and then changing it automatically to “CI”. Right now for the check_out view I’d just like to search for the Part() with the barcode, and update and log both tables appropriately.Eventually I’d like to be able to choose between other statuses that are listed in a drop down menu, but that’s for later =)
I ended up caching the status object here is my answer.