Hi Guys,
I'm really struggling with this:
I'm writing an application where there is a python script running separately to the Django web app. I am able to write to the Django Mysql DB via ORM - no problem there. Eventually this script will be scheduled.
The issue is that there are three tables: event, sessions, results, in the database, and I need to setup a one to many relationship. I have specified foreignkey fields in models.py but on retrieving the data it only returns the one record.
i.e.
A single event ---> Multiple Sessions ---> Multiple Results
I need a way to write the event id, to multiple sessions and session to results so that they are all linked. I feel that I need to retrieve the ID from the database in my script and then write that back into the next table. No matter what I do I cant get it to find the id and write it properly.
I think if I was using a Django view or template it would work but since im not - i'm jsut not sure how to achieve this.
Hopefully the below helps to explain it better.
Event
id Url title date
1 www. Test Event 22/01/18
2 www. Event 2 24/02/18
Sessions
id url date name time sessclass eventid_id
1 www. 22/01/18 test 2:00 qualify (**Should be Event table id field) e.g. 1
2 www. 22/01/18 test 3:00 heat1 Same date so event id should be 1
3 www. 24/02/18 test2 9:00 qualify Event ID filed should be 2
I cant use the date field, because an event might run over multiple dates, so I need to write the ID into the Sessions table as a part of the loop. I've pasted my code below
Independent Script File:
tk_events = sh.OrgEvents()
for org_event in tk_events:
events = sh.Events(org_event['url'])
print ('Event: ' + org_event['title'])
try:
db = dbevent.objects.create(url=org_event['url'], title=org_event['title'], date=org_event['date'])
except Exception as e:
pass
for event in events:
for sessions in event:
print ('-> Category: ' + sessions['title'])
for session in sessions['session']:
print (' -> Session: ' + session['name'])
#try:
db = dbsessions.objects.create(url=session['url'],sessclass=sessions['title'],name=session['name'],date=session['date'],time=session['time'])
#except Exception as e:
#pass
#db = dbsessions.objects.filter(url=session['url']).update(eventid_id=evtid['evtid'])
#for result in sh.Session(session['url']):
#resultindex = db.event.create_index("_id", unique=True)
#dbpost = db.result.insert_one(result)
Models.py
from django.db import models
class dbevent(models.Model):
url = models.CharField(max_length=250, unique=True)
title = models.CharField(max_length=250)
date = models.CharField(max_length=50)
class Meta:
unique_together = ("url", "title")
def __str__(self):
"""String for representing the Model object."""
return '%s %s' % (self.title, self.date)
class dbsessions(models.Model):
eventid = models.ForeignKey(dbevent, on_delete=models.CASCADE)
url = models.CharField(max_length=250)
date = models.CharField(max_length=50)
name = models.CharField(max_length=250)
time = models.CharField(max_length=50)
sessclass = models.CharField(max_length=250)
class Meta:
unique_together = ("url", "sessclass")
class dbresults(models.Model):
sessionid = models.ForeignKey(dbsessions, on_delete=models.CASCADE)
bestlap = models.CharField(max_length=5)
besttime = models.CharField(max_length=20)
competitorname = models.CharField(max_length=250)
diff = models.CharField(max_length=20)
laps = models.CharField(max_length=20)
position = models.CharField(max_length=20)
racenumber = models.PositiveSmallIntegerField
topspeed = models.CharField(max_length=20)
totaltime = models.CharField(max_length=20)
winner = models.BooleanField
[edit by admin: formatting]