简体   繁体   中英

Linking table in django

I have the tables below and I'm trying to link them such that I can query and perform calculation from fields of these tables.Example quantity - quantity_issued. Any help help would be highly appreciated.

class Stock(models.Model):
    item_date = models.DateField(("Date"), default=datetime.date.today)
    item_name = models.CharField(max_length=100, blank=True, null=True)
    quantity  = models.IntegerField(blank=True, null=True)
    quantity_expected = models.IntegerField(blank=True, null=True)
    total_cost = models.IntegerField(blank=True,null=True)
    supplier_details  = models.CharField(max_length=100, blank=True, null=True)
    created_by = models.CharField(max_length=100, blank=True, null=True)
    last_update = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return self.item_name + " " + str(self.quantity)


class Issued_items(models.Model):
    item_date = models.DateField(("Date"), default=datetime.date.today)
    item_name = models.CharField(max_length=100, blank=True, null=True)
    quantity_issued = models.IntegerField(blank=True, null=True)
    issued_to = models.IntegerField(blank=True, null=True)
    last_update = models.DateTimeField(auto_now_add=False, auto_now=True)
  
    def __str__(self):
         return self.item_name + " " + str(self.quantity_issued)

You may try adding to your Issued_items class:

 stock = models.ForeignKey(Stock, on_delete=models.CASCADE)

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM