Home > Net >  query set in django
query set in django

Time:11-30

i really need help with this: I have 3 models say User,Item and Comment where User is a foreign key in Item and Item is a foreign key in Comment. i want to get all comment belonging to a particular user in my view, how do i achieve it. bellow is are my model

 class User(models.Model):
        name= models.CharField(max_length=200,null=True)
        updated = models.DateTimeField(auto_now=True)
        created = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.name

 class Item(models.Model):
        user = models.ForeignKey(User, on_delete=CASCADE)
        name= models.CharField(max_length=200,null=True)
        updated = models.DateTimeField(auto_now=True)
        created = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.name

 class Comment(models.Model):
        user = models.ForeignKey(Item, on_delete=CASCADE)
        name= models.CharField(max_length=200,null=True)
        updated = models.DateTimeField(auto_now=True)
        created = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.name

CodePudding user response:

there is a problem of design model in your code.
here is how you can do it.

 class Comment(models.Model):
        item = models.ForeignKey(Item, on_delete=CASCADE)  # the related item
        author = models.ForeignKey(User,on_delete=CASCADE) #new ( the author of the comment)
        updated = models.DateTimeField(auto_now=True)
        created = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.name

now in your views.py you can do something like this.

Comment.objects.filter(author=request.user) 

CodePudding user response:

UPDATED 2

You should consider changing model, like amadou-sow said, that is right way to do such things

UPDATED

def get_user_comments(request):
    qs = Comment.objects.filter(user__user=request.user)

You could access yours related objects with "__" (double _) read more in official docs

And you really should rename "user" field to "item" in Comment model if it is foreign key to Item model, like this:

 class User(models.Model):
        name= models.CharField(max_length=200,null=True)
        updated = models.DateTimeField(auto_now=True)
        created = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.name

 class Item(models.Model):
        user = models.ForeignKey(User, on_delete=CASCADE)
        name= models.CharField(max_length=200,null=True)
        updated = models.DateTimeField(auto_now=True)
        created = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.name

 class Comment(models.Model):
        item = models.ForeignKey(Item, on_delete=CASCADE)
        name = models.CharField(max_length=200,null=True)
        updated = models.DateTimeField(auto_now=True)
        created = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.name

#view.py
def get_user_comments(request):
    return Comment.objects.filter(item__user=request.user)
  • Related