2016-08-28 17 views
0

私はDjangoで学びたいと思っています。私は在庫アプリケーションを準備しようとしています。しかし、私は入力と出力の製品を計算しません。Django製品合計

私は&というモデルを用意しています。

モデル:モデルStokGiris.miktariとStok.Cikis.miktariフィールドに位置

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 

Views.py

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/') 

計算を必要としています。 合計= StokGiris.miktari - StokCikis.miktariと私はレコードをリストしたい。

答えて

0

私があなたの質問を正しく理解している場合は、両方のモデルのmiktarの値を集計し、お互いから引き算します。あなたは再び何を意味するか本当にわからない...レコードを一覧表示については

from django.db.models import Sum 

total = StokGiris.objects.all().aggregate(Sum('miktar')) - StokCikis.objects.all().aggregate(Sum('miktar')) 

のが、「レコードを一覧表示」する方法をいくつか:あなたは、組み込みの集約ジャンゴでそれを行うことができます

# 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. 
関連する問題