Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8346323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:58:30+00:00 2026-06-09T06:58:30+00:00

Hi I’m writing an hardware inventory application, and I’d like to log each time

  • 0

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 =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:58:31+00:00Added an answer on June 9, 2026 at 6:58 am

    I ended up caching the status object here is my answer.

    #Signals.py
    @receiver(post_save, sender=Part)
    def log_entry(sender, instance, created, raw, *args, **kwargs):
        # Using the statuses from the above method to create a PartLog entry. 
        newStatus = instance.status
    
        if not created:
            # Get old status from current state
            oldStatus = instance._state.old_status
            PartLog.objects.create(part=instance,
                                   old_status=oldStatus,
                                   new_status=instance.status)
            instance._state.old_status = newStatus
        else:
            PartLog.objects.create(part=instance, old_status=Status(1), new_status=Status(3))
    
    #Models.py
    
    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)
    
        def __init__(self, *args, **kwargs):
            super(Part, self).__init__(*args, **kwargs)
            # Caching the existing Part object's status
            if self.pk:
                self._state.old_status = self.status
    
        def __unicode__(self):
            return unicode(self.bar_code)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.