开发者

Django queryset custom properties

开发者 https://www.devze.com 2023-04-10 06:51 出处:网络
I have user profile like class UserProfile(models.Model): completed_tasks = models.ManyToManyField(Tasks)

I have user profile like

class UserProfile(models.Model):

    completed_tasks = models.ManyToManyField(Tasks)

then task model

class Tasks(models.Model):

    <...fields...>

Then I want to filter some开发者_StackOverflow中文版 task and have a queryset property "completed" that marks if task objects is in user completed_tasks.

Example

t = Tasks.objects.filter(...).order_by(...)

t[0].completed # False

t[1].completed # True

Any ideas how can I do that?


Tricky question: in your model task completion depends on user having it in his collection, which makes the logic ambiguous. Is the task completed if any user completed it? Maybe all users need to complete it? Or maybe answer depends on user asking? Try making your models more defined.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号