[Fixed]-Django global object individual user relation

1👍

From what I understood in your text, this could be a way to build the 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 you can do queries like 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 you just check if the combination user-issue exists in the ReadIssue table. Of course it would be a good idea to add an index_together for that pair, because that’s how you will most likely do all look-ups in that table.

And by adding these little helper methods to the models, you can make the whole thing very expressive and easy to follow.

👤C14L

Leave a comment