Witam, mój problem jest taki, że chce stworzyć tabele z najlepszymi strzelcami ligi piłkarskiej, ale nie wiem jak połączyć 2 kolumny z liczbą goli w danym meczu, z liczbą goli w innym meczu tego samego zawodnika.
Informacje na temat ile goli, przez jakiego zawodnika i w jakim meczu znajduja sie w klasie info_mecz
prawdopodobnie muszę użyć Aggregation ale jak i co dokładnie to dalej nie wiem.
models.py
# -*- coding: utf-8 -*-
from django.db import models
import datetime
class Klub(models.Model):
Nazwa = models.CharField(max_length=50, blank=False, null=False, unique=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False,)
updated = models.DateTimeField(auto_now_add=False, auto_now=True,)
punkty = models.IntegerField(blank=False,null=True,)
porazki = models.IntegerField(blank=False,null=True,)
zwyciestwa = models.IntegerField(blank=False,null=True,)
remisy = models.IntegerField(blank=False,null=True,)
def __unicode__(self):
return self.Nazwa
class Zawodnik(models.Model):
nr_zawodnika = models.IntegerField(blank=False, null=False, unique=True)
imie = models.CharField(max_length=50, blank=False, null=False,)
nazwisko = models.CharField(max_length=50, blank=False, null=False,)
pozycja = models.CharField(max_length=50, blank=False, null=True,)
data_urodzenia = models.DateField(auto_now=False, auto_now_add=False,)
klub = models.ForeignKey(Klub)
def __unicode__(self):
return self.imie
class Trener(models.Model):
klub = models.ForeignKey(Klub)
imie = models.CharField(max_length=50, blank=False, null=False,)
nazwisko = models.CharField(max_length=50, blank=False, null=False,)
data_urodzenia = models.DateField(auto_now=False, auto_now_add=False,)
def __unicode__(self):
return self.imie.encode('ascii', errors='replace')
class Mecz(models.Model):
data = models.DateField(auto_now_add=True, auto_now=False,)
godz_spotkania = models.TimeField(auto_now_add=True, auto_now=False,)
updated = models.DateTimeField(auto_now_add=False, auto_now=True,)
nr_kolejki = models.IntegerField(blank=False,null=True,)
widownia = models.IntegerField(blank=False,null=True,)
gospodarz = models.ForeignKey(Klub,related_name='mecz_gospodarz')
gosc = models.ForeignKey(Klub,related_name='mecz_gosc')
czas_gry = models.TimeField(auto_now_add=True, auto_now=False,)
gole_gosci = models.IntegerField(blank=False,null=True,)
gole_gospodarz = models.IntegerField(blank=False,null=True,)
stadion_nr = models.IntegerField(blank=False,null=False,)
def __unicode__(self):
info = (str(self.gospodarz), 'kontra', str(self.gosc))
return str(info).encode('ascii', errors='replace')
class Info_mecz(models.Model):
gole_zawodnika = models.IntegerField(blank=False,null=True,)
zolte_kartki = models.IntegerField(blank=False,null=True,)
asysty = models.IntegerField(blank=False,null=True,)
stadion_nr = models.IntegerField(blank=False,null=False,)
nr_zawodnika = models.ForeignKey(Zawodnik)
nr_meczu = models.ForeignKey(Mecz)
def __unicode__(self):
return str(self.gole_zawodnika)
views.py
from django.conf import settings
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.db.models import F
from .forms import KlubForm
from .models import Klub, Zawodnik, Trener, Mecz, Info_mecz
# Create your views here.
def home(request):
form = KlubForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
Nazwa = form.cleaned_data.get("Nazwa")
if not instance.Nazwa:
Nazwa = ""
instance.Nazwa = Nazwa
instance.save()
if request.user.is_authenticated():
queryset = Klub.objects.order_by('-punkty')
ziomset = Zawodnik.objects.all()
setset = Info_mecz.objects.order_by('-gole_zawodnika')
Klub.objects.distinct()
Zawodnik.objects.distinct()
context = {
"queryset":queryset,
"ziomset":ziomset,
"setset":setset,
"form":form,
}
return render(request, "home.html", context,)