How to save to Django Model that Have Mulitple Foreign Keys Fields
        Posted  
        
            by Spikie
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Spikie
        
        
        
        Published on 2010-04-07T12:34:50Z
        Indexed on 
            2010/04/07
            15:43 UTC
        
        
        Read the original article
        Hit count: 323
        
I have Models for business Apps
class staff_name(models.Model): TITLE_CHOICES = ( ('Mr', 'Mr'), ('Miss', 'Miss'), ( 'Mrs', 'Mrs'), ( 'chief', 'chief'), ) titlename = models.CharField(max_length=10,choices=TITLE_CHOICES) firstname = models.CharField(max_length=150) surname = models.CharField(max_length=150) date = models.DateTimeField(auto_now=True) class meta: ordering = ["date"] get_latest_by = "date"
class inventory_transaction(models.Model):
stock_in = models.DecimalField(blank=True, null=True,max_digits=8, decimal_places=2)
stock_out = models.DecimalField(blank=True,null=True,max_digits=8, decimal_places=2)
Number_container = models.ForeignKey(container_identity)
staffs = models.ForeignKey(staff_name)
goods_details = models.ForeignKey(departments)
balance = models.DecimalField(max_digits=8, decimal_places=2)
date = models.DateTimeField(auto_now=True)
What i want to accomplish is
check if the staff have made entry to the table before if yes add the value for the stock in plus (last) balance field and assign to balance
if no just assign stock in value to balance field and save these are my codes
These are my codes:
try:
    s = staffname.staffs_set.all().order_by("-date").latest() # staffname is the instant of the  class model staff_name
    e = s.staffs_set.create(stockin=vdataz,balance=s.balance + vdataz )  # e is the instant of the class model inventory_transaction
    e.save
    e.staffs.add(s)
    e.from_container.add(containersno)
    e.goods_details.add(department)
except ObjectDoesNotExist:
    e = staff_name.objects.create(stockin=vdataz,balance=vdataz )
    e.save
    e.staffs.add(staffname)
    e.from_container.add(containersno)
    e.goods_details.add(department)
I will really appreciate a solution Thanks
I hope it make more sense now. iam on online if you need more explanation just ask in the comment.Thank you for your help
© Stack Overflow or respective owner