结合django用户和多对多模型,以按用户和其他字段组织显示

时间:2018-10-23 19:07:08

标签: django django-templates django-queryset manytomanyfield

我有一个代表日历事件的Django模型:

class Event(models.Model):
   title = models.CharField(max_length=120)
   date = models.DateField(default=default_event_date)
   category = models.ForeignKey(Category)
   users = models.ManyToManyField(User, related_name='events',
                                          limit_choices_to={'is_staff': True})
   ...

如何从该模型中获取查询集并组织数据,以便可以在模板中显示它:

       Date1         Date2         Date3         Date4      ...
User1  CatA (Title1) CatC (Title4) CatB (Title7) -
User2  CatD (Title2) CatB (Title5  -             CatD (Title9)
User3  CatC (Title3) CatC (Title4) CatC (Title8) CatC (Title10)
User4  -             -             -           -
User5  -             CatA (Title6) -             CatD (Title9)
...

DateN是date字段,UserN是users字段,CatX是category字段,TitleN是title

0 个答案:

没有答案