[英]Django save multiple data with same form - different data for different users
Now I have a model called Score, whose forms.py as below, I would like to create different score for different users once, now I only can create a score for a user one by by, and I need to create many times, is there a easy way to create score for users just once? 现在,我有一个名为Score的模型,其forms.py如下所示,我想一次为不同的用户创建不同的分数,现在我只能为一个用户创建一个分数,并且需要多次创建,有没有一种简单的方法可以为用户一次创建分数? Thank you so much for any advice. 非常感谢您的任何建议。
class Score(models.Model):
test = models.ForeignKey(Test, null=True, blank=True, on_delete=models.CASCADE)
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
score = models.FloatField(default=0,)
pass_score = models.FloatField(default=0,)
class ScoreForm(forms.ModelForm):
class Meta:
model = TestScore
fields = ('test', 'user', 'score', 'pass_score')
If what you want is to hit url like /all_users/test1/
如果您想要的是像/all_users/test1/
这样的网址
and get a set of forms (each row is a form) like this: 并获得一组这样的表单(每一行都是一个表单):
|user|test1 score |
| u1 | 80% |
| u2 | 72% |
| u3 | __ |
then what you want is a formset or, even closer to what you're doing, a modelFormset . 那么您想要的是一个formset,或者甚至更接近您正在做的是modelFormset 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.