I have a queryset created by objects.filter() and joins 2 tables.
How do I then save this result to a different table?
Below is the example I am trying to work on. The queryset filter is working as expected.
I am now lost on how to write some of the fields into a different table and would welcome some guidance.
Models.py
#holds the race info
class Race(models.Model):
event = models.ForeignKey(
'Event',
related_name = 'race_in_event',
on_delete=models.PROTECT,
)
stage_number = models.IntegerField()
schedule = models.ForeignKey(
'Schedule',
related_name='race_schedule',
on_delete=models.PROTECT,
blank=True, null=True,
)
race_number = models.IntegerField()
sort_order = models.IntegerField()
group = models.IntegerField()
race_status = models.CharField(
max_length=3,
choices=type_choices,
)
#updated during racing, holds live position
class RaceData(models.Model):
race = models.ForeignKey(
'Race',
related_name = 'racedata_in_race',
on_delete=models.PROTECT,
)
flight = models.ForeignKey(
Flight,
related_name = 'flight_in_racedata',
on_delete=models.PROTECT,
)
boat = models.ForeignKey(
Boat,
related_name = 'boat_in_racedata',
on_delete=models.PROTECT,
)
timestamp = models.DateTimeField(auto_now_add=True)
event_type = models.CharField(
max_length=3,
choices=type_choices,
)
position=models.IntegerField(blank=True, null=True)
notes=models.TextField(blank=True, null=True)
#a point in time snapshot of race positions generated after finish
class RaceScores(models.Model):
race = models.ForeignKey(
'Race',
related_name = 'score_for_race',
on_delete=models.PROTECT
)
boat = models.ForeignKey(
'Boat',
related_name = 'score_for_boat',
on_delete=models.PROTECT
)
flight = models.ForeignKey(
'Flight',
related_name='score_for_flight',
on_delete=models.PROTECT
)
team = models.ForeignKey(
'Team',
related_name='score_for_team',
on_delete=models.PROTECT
)
position = models.IntegerField()
scoring_code = models.CharField(max_length=25, blank=True)
views.py
# for the specified event_id get all the race where status=fin and get all related race_data
racelist = Race.objects.filter(event_id=event_id, race_status='fin').prefetch_related('racedata_in_race').all()
# with this result set, write race.id, racedata.position, racedata.boat_id, racedata.flight_id to the RaceScores table
Many thanks
Paul