Hi, I am working on a multi user site, Coaches and Athletes. I have created two profiles and abstracted the user model to include is_coach or is_athlete. Within the site, I need coaches to upload .csv files with athletes data that will be imported into the respective models depending on the file and activity. files are specific to individual athletes
My problem is with the logic to allocate an athlete to the specific uploaded data. I have a form for uploading the file which works fine. I am trying to use the view to manage the changes and add an athlete id to the data. code below. Any help on this would be appreciated. Thanks
csv models.py
from django.db import models
class Csv(models.Model):
file_name = models.FileField(upload_to = 'athletes')
uploaded = models.DateTimeField(auto_now_add=True)
activated = models.BooleanField(default=False)
def __str__(self):
return f"File id: {self.id}"
csv forms.py
from django import forms
from .models import Csv
class CsvModelForm(forms.ModelForm):
class Meta:
model = Csv
fields = ('file_name', )
csv views.py is where I think the joining between models needs to take place
I would like to use a dropdown displaying all the athletes allocated to the specific coach profile. In this example the model is for daily health and wellness metrics
from django.shortcuts import render
from .forms import CsvModelForm
from .models import Csv
import csv
from athletes.models import Metrics
from Profiles import Coach, Athlete
def upload_file_view(request):
form = CsvModelForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
form = CsvModelForm()
obj = Csv.objects.get(activated=False)
with open(obj.file_name.path, 'r') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if 1==0:
pass
else:
date = row[1]
metric = row[2]
value = row[3]
return render(request, 'uploader/uploader.html', {'form': form})
The Athlete and Coach models for the different profiles are here
class User(AbstractUser):
is_athlete = models.BooleanField(default=False)
is_coach= models.BooleanField(default=False)
is_admin= models.BooleanField(default=False)
class Coach(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
def __str__(self):
return self.username
class Team(models.Model):
coach = models.ManyToManyField(Coach)
class Athlete(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
date_of_birth = models.DateField()
team = models.ForeignKey(Team, on_delete=models.CASCADE)
def __str__(self):
return self.username