Django Product Total
<p>I'm trying to learn in Django. I'm trying to prepare the inventory application. But I do not calculate the input and output products. </p>

<p>I prepare models &amp; views.</p>

<p><strong>Models:</strong></p>

<pre><code>class Kategori(models.Model):
    adi = models.CharField(max_length=10, verbose_name="Kategori")

    def __str__(self):
        return self.adi

class Birim(models.Model):
    birim = models.CharField(max_length=2, verbose_name="Birim")

    def __str__(self):
        return self.birim

class Urunler(models.Model):
    adi = models.CharField(max_length=50, verbose_name="ÃrÃ¼n AdÄ±")
    kod = models.PositiveSmallIntegerField(verbose_name="ÃrÃ¼n Kodu", blank=True, null=True)
    etkenMadde = models.CharField(max_length=100, verbose_name="Ä°Ã§erik Etken Madde", blank=True, null=True)
    tarih = models.DateField(default=datetime.now(), editable=False)
    birim = models.ForeignKey(Birim, verbose_name="Birim")
    kategori = models.ForeignKey(Kategori, verbose_name="Kategori")
    aciklama = models.CharField(max_length=50, verbose_name="AÃ§Ä±klama", blank=True, null=True)

    def __str__(self):
        return self.adi


class StokCikis(models.Model):
    urun = models.ForeignKey(Urunler, related_name="scikis_urun", verbose_name="ÃrÃ¼n")
    tarih = models.DateTimeField(default=datetime.now())
    miktar = models.PositiveSmallIntegerField(verbose_name="Miktar", default=0)
    teslimAlan = models.CharField(max_length=20, verbose_name="Teslim Alan")
    tesimEden = models.CharField(max_length=20, verbose_name="Teslim Eden")

    def __str__(self):
        return self.urun.adi

class StokGiris(models.Model):
    urun = models.ForeignKey(Urunler, related_name="sgiris_urun", verbose_name="ÃrÃ¼n")
    tedarikci = models.CharField(max_length=100, verbose_name="TedarikÃ§i", blank=True, null=True)
    irsaliyeNo = models.PositiveSmallIntegerField(verbose_name="Ä°rsaliye No", blank=True, null=True)
    tarih = models.DateField(default=datetime.now().strftime("%d.%m.%Y"))
    miktar = models.PositiveSmallIntegerField(verbose_name="Miktar", default=0)
    aciklama = models.CharField(max_length=100, verbose_name="AÃ§Ä±klama", blank=True, null=True)

    def __str__(self):
        return self.urun.adi
</code></pre>

<p><strong>Views.py</strong></p>

<pre><code>def kategori(request):
    kategori = Kategori.objects.all()
    return render_to_response('stok_kategoriler.html', locals())
def kategoriEkle(request):
    kategoriId = request.GET.get('id')
    if kategoriId:
        ktgr = Kategori.objects.get(pk=kategoriId)
        form = KategoriForm(instance=ktgr)
    else:
        form = KategoriForm
    if request.method == 'POST':
        if kategoriId:
            form = KategoriForm(request.POST, instance=ktgr)
        else:
            form = KategoriForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/stok/kategoriler')
    button = "Ekle"
    baslik = "Kategoriler"
    return render(request, 'stok_kategori_ekle.html', locals())
def kategoriSil(request):
    kategoriId = request.GET.get('id')
    kategori = Kategori.objects.get(pk=kategoriId)
    kategori.delete()
    return HttpResponseRedirect('/stok/kategoriler')
def stokBirimler(request):
    birimler = Birim.objects.all()
    return render_to_response('stok_birimler.html',locals())
def stokBirimEkle(request):
    birimId = request.GET.get('id')
    if birimId:
        stok_birim = Birim.objects.get(pk=birimId)
        form = BirimForm(instance=stok_birim)
    else:
        form = BirimForm()
    if request.method == 'POST':
        if birimId:
            form = BirimForm(request.POST, instance=stok_birim)
        else:
            form = BirimForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/stok/birimler/')
    baslik = "Stok Birimleri"
    return render(request, 'stok_birim_ekle.html', locals())
def stokBirimSil(request):
    birimId = request.GET.get('id')
    birim = Birim.objects.get(pk=birimId)
    birim.delete()
    return HttpResponseRedirect('/stok/birimler/')
def stokUrunler(request):
    urunler = Urunler.objects.all()
    return render_to_response('stok_urunler.html', locals())
def urunEkle(request):
    urunId = request.GET.get('id')
    if urunId:
        stok_urun = Urunler.objects.get(pk=urunId)
        form = UrunForm(instance=stok_urun)
    else:
        form = UrunForm()
    if request.method == 'POST':
        if urunId:
            form = UrunForm(request.POST, instance=stok_urun)
        else:
            form = UrunForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/stok/urunler/')
    baslik = "Stok ÃrÃ¼nleri"
    return render(request, 'stok_urun_ekle.html', locals())
def urunSil(request):
    urunId = request.GET.get('id')
    urun = Urunler.objects.get(pk=urunId)
    urun.delete()
    return HttpResponseRedirect('/stok/urunler/')
</code></pre>

<p>Located in the model StokGiris.miktari and Stok.Cikis.miktari fields need calculation. <strong>Total = StokGiris.miktari - StokCikis.miktari</strong> and I want to list the records.</p>

<p>If I understand your question correctly, you want to aggregate the values of <code>miktar</code> of both models and subtract them from each other. You can do it with Django built-in aggregation:</p>

<pre><code>from django.db.models import Sum

total = StokGiris.objects.all().aggregate(Sum('miktar')) - StokCikis.objects.all().aggregate(Sum('miktar'))
</code></pre>

<p>As for listing the records... Not really sure what you mean again, but some ways to "list the records":</p>

<pre><code># Just a list of DB items
list_of_stokgiris = list(StokGiris.objects.all())

# Serialized list of dictionaries with the DB values
list_of_stokcikis = StokCikis.objects.values()  # You can add an argument to specify which fields you want to be serialized

# Serialized list of tuples with DB values
# I.e. [('Filipp',), ('Artjom',), ('Marat',)]
list_of_urunler = Urunler.objects.values_list('adi')  # Same here for the argument. Multiple args are also supported.
</code></pre>

python django