简体   繁体   中英

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:

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:

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. 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 . This is where you want to read about your goal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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