0

我想要一个简单的东西:

class MyModel(models.Model):
    "
    month field is enforced to first day of month, so I
    can have an entry by user and by month only.
    "
    user = models.ForeignKey(User)
    month = models.DateField()
    field = models.IntegerField(default=0)
    is_ok = models.BooleanField(default=True)
    other_field = models.IntegerField(default=0)

    class Meta:
        unique_together = (("user", "month"),)

所以我想为一个给定的月份预加载(就像 select_related 会做的那样)我的用户查询集条目。

# at the end it may for exemple be something like that....
user_with_related = User.a_related_manager.get_with_my_model_for_date(my_date)
for u in user_with_related:
    if not u.current_my_model:
        MyModel.create_for_user(u)
    elif not u.current_my_model.is_ok:
        u.current_my_model.rebuild()
 return render(request, "my_template.html", {"users": user_with_related})

并非每个用户都链接了给定月份的 MyModel 实例。

任何提示?

4

0 回答 0