我有三个模型User
(django.contrib.auth)Screening
和User_Screening
. 这User_Screening
是一个带有额外字段的 m2m 表status
。
#models.py
from django.db import models
from django.contrib.auth.models import User
class Screening(models.Model):
title = models.CharField(max_length=255)
start = models.DateTimeField()
user_relation = models.ManyToManyField(User, blank=True,
through='User_Status')
class User_Status(models.Model):
ATTENDING = 'c'
NOT_ATTENDING = 'n'
PROJECTION = 'p'
STATUS_CHOICES = (
(ATTENDING, 'attending'),
(NOT_ATTENDING, 'not attending'),
(PROJECTING, 'projecting'),
)
screening = models.ForeignKey(Screening)
user = models.ForeignKey(User)
status = models.CharField(max_length=1, choices=STATUS_CHOICES)
现在我想制作一个视图,显示所有即将上映的电影。到目前为止,很容易:
#views.py
@login_required()
def index(request):
current_screenings = Screening.objects.filter(start__gte=timezone.now())
context = {'current_screenings': current_screenings}
return render(request, 'schedule/index.html', context)
在这个视图中,登录的用户应该能够更新他们的status
(从User_Screening
表中)。也可能是用户还没有此筛选的记录,因此应该创建一个。
我不明白,我如何为每个筛选存档一个表单下拉字段,用户可以在其中选择他的状态。(?
如果尚未设置状态attending
,not attending
或projection
)
据我了解,我需要多种表格,这些表格都知道它们与哪些筛选有关。
此外,Formsets似乎不起作用,因为我不能总是用初始数据填写表格,因为某些或所有筛选可能会丢失记录。此外,我不知道哪种形式属于哪种筛选对象。
更新: 我想在 HTML 中得到的结果是这样的:
<form>
<h1>Current Screening 1</h1>
<select onchange="submit()" name="screening_user" id="s1">
<option value="att">Attending</option>
<option value="not_att">Not Attending</option>
<option selected="selected" value="pro">Projection</option>
</select>
<h1>Current Screening 2</h1>
<select onchange="submit()" name="screening_user" id="s2">
<!-- The 'Please Select' option is only visible, if the user does not
have a relation in 'User_Screening' for this screening -->
<option selected="selected" value="none">Please Select</option>
<option value="att">Attending</option>
<option value="not_att">Not Attending</option>
<option value="pro">Projection</option>
</select>
<!-- More Screenings -->
<h1>Current Screening n</h1>
<!-- select for screening n -->
</form>
因此,需要根据登录用户从具有预加载数据的相同表单中更改表单数量。