我想在Django学习。我正在尝试准备库存应用程序。但我不计算输入和输出产品。
我准备模特&视图。
型号:
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和Stok.Cikis.miktari字段需要计算。 总计= StokGiris.miktari - StokCikis.miktari ,我想列出记录。
答案 0 :(得分:0)
如果我正确理解了您的问题,您希望汇总两个模型的miktar
值,并相互减去它们。您可以使用Django内置聚合:
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.