django global object individual user relation -
i have 2 main models, comic , issue, single comic has multiple issues. trying figure out how issues specific user has read. thinking of creating model called readissue store read state can't figure out how read property on each issue depending on user.
from understood in text, way build basic models
class comic(models.model): name = models.charfield(max_length=100) class issue(models.model): comic = models.foreignkey(comic, related_name='issues') title = models.charfield(max_length=100) def was_read_by(self, user): return bool(self.users.filter(user=user).count()) class readissue(models.model): user = models.foreignkey(user, related_name='read') issue = models.foreignkey(issue, related_name='users')
now can queries this
user = users.objects.get(pk=1) comic = comic.objects.get(pk=1) first_issue = comic.issues.all().first() user_read_it = first_issue.was_read_by(user)
so check if combination user-issue
exists in readissue
table. of course idea add index_together
pair, because that's how look-ups in table.
and adding these little helper methods models, can make whole thing expressive , easy follow.