Skip to main content

Querying the database

A student wanted to join 2 tables and display the results but he was not making any headway and couldn't figure out what was wrong with his code. After some debugging via printing to the console, I realised that he was not getting any data and that's why nothing was being displayed on his template. And there was a simple reason for this, his model was not set up to allow such a query. He was joining a table that had no foreign keys to various other tables that were linked to each other rather than to the first table. 

class Players(models.Model):
PlayerName = models.CharField(max_length=100)
PlayerAddress = models.CharField(max_length=100)
PlayerEmailAddress = models.CharField(max_length=100)
PlayerPhoneNumber = models.IntegerField()
TeamID = models.ForeignKey(Team, on_delete=models.CASCADE)
Coach = models.ForeignKey(Coaches, on_delete=models.CASCADE)

I did what I always do in such situations where I encounter problems I have not seen before, I pulled his branch and started working on my own solution. I changed the table he was joining and added another foreign key to that table to ensure that I could get all the data I wanted. This small exercise made me realise just how important it is to design your database well. 

def team_home(request):
jointables= Players.objects.select_related('TeamID', 'Coach')
playerQueryset = jointables.order_by('-TeamID', 'PlayerName')
context = {'team' : playerQueryset}
return render(request, 'SoccerApp/SoccerApp_page.html', context)

Printing to the console showed me I was getting all the data I wanted to, and then it was just a matter of passing it to the template. This was fun. I really should play with the database more often!










Comments

Popular posts from this blog

Certified CSS Surgeon

I've just been certified as a top notch CSS Surgeon 😹😹😹. Codepip has just officially launched and they're offering CSS Surgeon for free until the 1st of September. I've already finished Flexbox Froggy(Free) and Grid Garden(Also free). I really do enjoy the CSS games on this website. It has helped me better understand CSS. There is still so much to learn. I've been trying to create a simple language learning website and it has been a struggle. I'm using the most simple website layout and yet it's just kicking my butt(I'm looking at you Nav bar). I hope to add some Javascript to make it more interactive in the future. At one point I really wanted to throw in the towel because it felt like I would never get this. It's hard not to give up when there are so many hurdles to cross. Can I really be confident at coding after just a 6 month bootcamp?

Deviants in a normal world

It's definitely been a bit since I've seen this graphy. Anyone who has learnt about standard deviation knows this graph. Standard Deviation Standard deviation shows us how spread out all the values in a set are from the mean. The higher the standard deviation, the more spread out the values are over a wider range and the flatter this curve. In a normal distribution, most values are within 1 standard deviation from the mean(the green part of the graph). Apparently NumPy can calculate standard deviation too! import numpy numSet = [ *lots of numbers* ] numSetStdDev = numpy.std(numSet) Variance The variance also indicates how spread out the values in a set are. It measures the average degree to which each value differs from the mean. variance = standard deviation ^2 import numpy numSet = [ *lots of numbers * ] numSetVar = numpy.var(numSet) Source:  https://www.w3schools.com/python/python_ml_standard_deviation.asp

Portfolio on GitHub

As part of the bootcamp, I've been building a portfolio website based on a template provided by the school. Honestly, I hate the template. It is hideous. But I guess it's a great starting point for someone who is unsure about what to do. It was also an opportunity for me to practice publishing a website online. Unfortunately, I had to pay for a webhosting service. After submitting my third update to my portfolio website, one of the instructors suggested that I could use GitHub Pages instead. *mindblown* This is amaziiiiing. And it's so simple. When I finally get down to creating my own portfolio from scratch, I am definitely going to put it up on GitHub instead. Also, speaking of GitHub, I'm slowly understanding how GitHub functions. It is taking me awhile. Especially because I don't use many of the functions that are available. I look forward to learning more and becoming a GitHub extraordinaire :D JK