skip to Main Content

I have a old league web site php script with mysql database from 2006. This script not running depends to old php and mysql versions. Thats why I tried to get data from mysql database using django but I have a problem with match listing page.

At database there is a table for league matches. I can fetch data and listing from using django template system like this :

models.py

 class LeagueMatch(models.Model):
objects = None
idm = models.AutoField(primary_key=True)
league = models.IntegerField()
ids = models.PositiveIntegerField()
judge = models.PositiveIntegerField()
date = models.IntegerField()
idg = models.PositiveIntegerField(blank=True, null=True)
pos = models.IntegerField(blank=True, null=True)
rnd = models.PositiveIntegerField(blank=True, null=True)
idc1 = models.IntegerField()
idc2 = models.IntegerField()
type = models.CharField(max_length=20, blank=True, null=True)
idpt = models.PositiveSmallIntegerField()
points1 = models.IntegerField()
points2 = models.IntegerField()
frags1 = models.SmallIntegerField()
frags2 = models.SmallIntegerField()
win1 = models.PositiveIntegerField()
draw = models.PositiveIntegerField()
win2 = models.PositiveIntegerField()
maps = models.CharField(max_length=70)
scores = models.CharField(max_length=70)
descr = models.TextField()
server = models.CharField(max_length=30)

views.py

def all_matches(request):
match_list = LeagueMatch.objects.all()
return render(request, 'home_match.html',
              {'match_list': match_list})

{% for match in match_list reversed %}
{% if match.league == 1 %}
        <div class="card" >
              <div class="card-body">
                 <h5 class="card-title"><a href="{% url 'match-detail' match.pk %}"> Match  No {{ match.idm }} - Team A ({{ match.idc1 }}) vs Team B ({{ match.idc2 }})</a></h5>
                <p class="card-text"><b>Score : {{ match.win1 }} : {{ match.win2 }}</b></p>
              </div>
        </div>
        </br></br>
{% endif %}

{% endfor %}

league matches table

Template render results like this :

All Matches

Match No 55 - Team A (9) vs Team B (7)

Score : 1 : 2


Match No 53 - Team A (9) vs Team B (2)

Score : 2 : 0

This is ok but team names are not in same table (league_match), there is only team number (idc1 and idc2). Team names are stored in another table name is league_clan. I want to display all matches list like this :

Match No 55 - Team A (team 9's name ) vs Team B (team 7'name)

Score : 1 : 2

I mean idc1 == 9 we will take data from other table what is the team number 9’s name and display. How can I write a model or template code for this display? I couldn’t do that with in {% for %} codes. Somethings wrong, need help. I hope explained thank you.

teams table with names(tags)

2

Answers


  1. Chosen as BEST ANSWER

    Hi thank you but getting errors like this :

    OperationalError at /matches/

    (1054, "Unknown column 'league_match.idc1_id' in 'field list'")

    Request Method: GET Request URL: http://localhost:8000/matches/ Django Version: 3.2.2 Exception Type: OperationalError Exception Value:

    (1054, "Unknown column 'league_match.idc1_id' in 'field list'")

    Exception Location: /home/tolga/.local/lib/python3.8/site-packages/MySQLdb/connections.py, line 259, in query Python Executable: /usr/bin/python3 Python Version: 3.8.5 Python Path:

    ['/home/tolga/q2ligi2006', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/tolga/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']

    Server time: Thu, 03 Jun 2021 09:16:13 +0000

    I updated models with ForeignKey field like this :

    class LeagueMatch(models.Model):
    objects = None
    idm = models.AutoField(primary_key=True)
    league = models.IntegerField()
    ids = models.PositiveIntegerField()
    judge = models.PositiveIntegerField()
    date = models.IntegerField()
    idg = models.PositiveIntegerField(blank=True, null=True)
    pos = models.IntegerField(blank=True, null=True)
    rnd = models.PositiveIntegerField(blank=True, null=True)
    idc1 = models.ForeignKey(LeagueClan, on_delete=models.CASCADE, related_name='teama')
    idc2 = models.ForeignKey(LeagueClan, on_delete=models.CASCADE, related_name='teamb')
    type = models.CharField(max_length=20, blank=True, null=True)
    idpt = models.PositiveSmallIntegerField()
    points1 = models.IntegerField()
    points2 = models.IntegerField()
    frags1 = models.SmallIntegerField()
    frags2 = models.SmallIntegerField()
    win1 = models.PositiveIntegerField()
    draw = models.PositiveIntegerField()
    win2 = models.PositiveIntegerField()
    maps = models.CharField(max_length=70)
    scores = models.CharField(max_length=70)
    descr = models.TextField()
    server = models.CharField(max_length=30)
    

    what is wrong thank you?


  2. Convert idc1 & idc2 from integerfield to ForeignKey field and then you can get the name by {{match.idc1.name}} and if it gets slow, you can use select_related.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search