简体   繁体   English

Django中过滤数据的问题

[英]Problems with filtering data in Django

I am making a post, and the question is that i need to filter a list with the users who have published the most posts.我正在发帖,问题是我需要过滤发布最多帖子的用户列表。 I had tried, but only managed to filter the total posts, and have had trouble with that.我试过,但只设法过滤了所有帖子,并且遇到了麻烦。

The fields in Models.py from User:来自用户的Models.py的字段:

from django.contrib.auth.models import AbstractUser
    
class Usuario(AbstractUser):
        email = models.EmailField(unique=True)
        creation_time = models.TimeField(auto_now_add=True)
        description = models.TextField()
        avatar = models.ImageField(blank=True, null=True, upload_to='autores/', default='img/default.jpg')
        social = models.URLField(blank=True, null=True)
        slug = models.CharField(max_length=60, unique=True, blank=True)
        is_author = models.BooleanField(default=False)

meanwhile in models.py from Blog:同时在来自博客的models.py

class Post(ModeloBase):
    title = models.CharField('Titulo del Post', max_length=200, unique=True)
    slug = models.SlugField(max_length=200, blank=True, unique=True)
    description = models.TextField('Descripcion')
    author = models.ForeignKey(Usuario, on_delete= models.CASCADE)
    category= models.ForeignKey(Category, on_delete= models.CASCADE)
    content= RichTextField()
    referential_img = models.ImageField('Imagen Referencial', upload_to='imagenes/', max_length=255)
    published= models.BooleanField('Publicado / No Publicado', default=False)
    publication_date = models.DateField('Fecha de Publicacion')
    like = models.ManyToManyField(Usuario, blank=True, related_name='likes')
    dislike = models.ManyToManyField(Usuario, blank=True, related_name='dislikes')

    class Meta:
        verbose_name = 'Post'
        verbose_name_plural = 'Posts'
    
    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Post, self).save(*args, **kwargs)

Looking in the django documentation, i tried to do the filtering but it didn't work for me.查看 django 文档,我尝试进行过滤,但对我不起作用。 I don't know what my mistake was, so i hope someone could help me.我不知道我的错误是什么,所以我希望有人能帮助我。

for example, so i have my structure to get recent posts by category:例如,我的结构可以按类别获取最近的帖子:

def get(self, request, *args, **kwargs):
        template = 'home.html'
        post_general = Post.objects.filter(state= True, published= True, category= Category.objects.get(name='General')).order_by('publication_date')

        context = {
            'post_general': post_general,
        }
        return render(request, template, context)

You could try something like this:你可以尝试这样的事情:

Usuario.objects.annotate(num_posts=Count('post')).order_by('-num_post')

If that does not answer your question properly (I had some problem understanding it), please refer to the django doc page on Aggregation .如果这不能正确回答您的问题(我在理解它时遇到了一些问题),请参阅Aggregation上的 django 文档页面。 This is where you want to read about your goal.这是您想要了解您的目标的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM