Django - 对多个标准的评级:模型表单集?
我想根据多个标准对某个内容进行评分。
想象一下这种模型:
class Content(models.Model):
name = models.CharField(max_length=50)
class Criteria(models.Model):
name = models.CharField(max_length=50)
content = models.ForeignKey(Content)
class ContRate(models.Model):
user = models.ForeignKey(User, help_text="Who rated ?")
crit = models.ForeignKey(Criteria)
rate = models.DecimalField()
用户有一个显示内容的页面。
在此页面上,他还可以根据标准集对内容进行评分
问题是:
您是否建议使用 模型表单集这个目的?
或者我应该做一个简单的 Ajax 表单来发布评级?
我为什么要这样做?
I have a content that I'd like to rate on multiple criteria.
Imagine this kind of model:
class Content(models.Model):
name = models.CharField(max_length=50)
class Criteria(models.Model):
name = models.CharField(max_length=50)
content = models.ForeignKey(Content)
class ContRate(models.Model):
user = models.ForeignKey(User, help_text="Who rated ?")
crit = models.ForeignKey(Criteria)
rate = models.DecimalField()
The user has a page displaying the content.
From this page, he can also rate the content on the criterias set
Question are:
Do you suggest to use a Model Formset for this purpose ?
Or should I do a simple Ajax form to post the ratings ?
Any why should I do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会使用 模型表单集 来实现这种类型问题虽然您不会使用
my_formset.is_valid()
也不会使用my_formset.save()
但因为它简化了视图中的表单构造和模板中的渲染。无需担心表单前缀等。
您对
onclick
事件(通过点击星星触发)的Ajax
调用应使用ContRate 调用视图
pk
(如果存在)和rate
作为参数。该视图将实例化一个带有这些参数的
ContRateForm
(与之前的modelformset_factory
中使用的相同),使用ModelForm
验证和数据库插入的常用机制/update 并最终呈现json
响应。I would use a model formset for this kind of problem although you are not going to use
my_formset.is_valid()
normy_formset.save()
but because it ease the forms construction in the view and the render in the template.No need to worry with the form prefixes etc.
Your
Ajax
call on theonclick
event (fired by a click on a star) should call a view with theContRate
pk
(if present) and therate
as parameters.The view will instanciate a
ContRateForm
(the same used in your previousmodelformset_factory
) whith those parameters, use the usual mechanisms ofModelForm
validation and database insert/update and finally render ajson
response.