我正在尝试开发一个与每次锻炼相关的 Django 应用程序。
我目前有两个表,一个存储锻炼(每次锻炼多个锻炼)信息,另一个存储锻炼信息,锻炼是两者之间的外键。
但是,我很难理解在一个表单中提交多个练习的概念,而不会很快变得相当丑陋。
请记住,我是 Django 菜鸟,因此请随时指出您看到的任何多余或无用的代码
模型.py
class workout(models.Model):
user = models.ForeignKey(User , db_column='userid')
datesubmitted = models.DateField()
workoutdate = models.DateField();
bodyweight = models.FloatField(null=True);
totalreps = models.IntegerField()
totalweight = models.FloatField()
numsets = models.IntegerField();
numexercises = models.IntegerField()
workoutname = models.CharField(max_length=250)
def getallexercises (self,id):
all_workout_exercises = exercise.objects.filter(workoutid = id)
return all_workout_exercises
def get_absolute_url(self):
return "/home"
class workoutForm(ModelForm):
class Meta:
model = workout
exclude = ('user','bodyweight','totalreps','totalweight','datesubmitted')
class exercise(models.Model):
workout = models.ForeignKey(workout,db_column='workoutid')
userid = models.IntegerField();
exercisename = models.CharField(max_length = 100)
repetitions = models.IntegerField()
weight = models.FloatField()
def get_absolute_url(self):
return "/home"
class exerciseForm(ModelForm):
class Meta:
model = exercise
视图.py
class workoutCreate(CreateView):
form_class = workoutForm
model = workout
def form_valid(self,form):
createxercise(self.request,self.model.pk)
form.instance.user = self.request.user;
form.instance.bodyweight = self.request.POST['weight']
form.instance.datesubmitted = datetime.datetime.now()
form.instance.totalweight = summation(list(self.request.POST['weightinputboxes']))
form.instance.totalreps = summation(list(self.request.POST['repinputboxes']))
return super(workoutCreate, self).form_valid(form)
我应该在哪里尝试提交练习?我已经阅读了有关inlineformset_factory 的信息,但我不确定是否可以通过这项工作在我的表中创建多个练习行......
感谢各位大侠的帮助,不胜感激!