代码之家  ›  专栏  ›  技术社区  ›  Valentin Golev

Django管理员不显示所有实体

  •  5
  • Valentin Golev  · 技术社区  · 15 年前

    我继承了用Django创建的应用程序。它有一个问题:在管理界面中,页面列出的不是所有实体(视频),而是一些实体(25个中的16个)。我不知道,这是什么。

    然后我跑步 python manage.py shell 那里 Video.objects.all() ,共有25个对象(使用 len 通过迭代 for 循环)。

    我找不到经理或其他什么(也许我只是不知道在哪里找他们)。

    在管理页面底部: 25 videos ,而只有16行。

    然后我添加到videomodeladmin类 list_per_page = 10 ,分页器显示三页,但只有前两页有任何视频,第三页没有显示行。

    这是一些代码。

    # admin.py
    class VideoModelAdmin(admin.ModelAdmin):
        list_display = ['title', 'short_desc', 'author', 'redactor_choise', 'views_num', 'rating', 'is_published']
        list_filter = ['is_published', 'redactor_choise']
        list_per_page = 10
        actions = ['make_published', 'update_comments_count']
        exclude = ('file_lq', 'file_hq', )#'thumb',)
    
        def make_published(self, request, queryset):
            queryset.update(is_published=1)
        make_published.short_description = "Опубликовать выделенные"
    
        def save_model(self, request, obj, form, change):
            instance = form.save(commit=False)
            instance.author = request.user
            instance.save()
            return instance
    
        def update_comments_count(self, request, queryset):
            for video in queryset:
                video.update_comments_count()
        update_comments_count.short_description = "Пересчитать комментарии!"
    
    
    # later there
    admin.site.register(Video, VideoModelAdmin)
    
    
    # models.py
    class Video(models.Model):
        def make_upload_path(instance, filename):
            return 'video/thumbs/' + generate_random_name(filename)
    
        category = models.ManyToManyField(Category, limit_choices_to = {'is_published': 1})
        title = models.CharField(max_length=128)
        short_desc = models.CharField(max_length=255)
        long_desc = tinymce_models.HTMLField(blank=True)
        file_lq = models.FileField(upload_to='video/lq/', null=True, blank=True)
        file_hq = models.FileField(upload_to='video/hq/', null=True, blank=True)
        thumb = models.FileField(upload_to=make_upload_path, blank=True, null=True)
        #thumb = fields.ThumbnailField(upload_to=make_upload_path, sizes=settings.VIDEO_THUMB_SIZE, blank=True, null=True)
        author = models.ForeignKey(User, editable=False)
        redactor_choise = models.BooleanField(default=False)
        views_num = models.SmallIntegerField(default=0, editable=False)
        comments_num = models.SmallIntegerField(default=0, editable=False)
        rating = models.SmallIntegerField(default=0, editable=False)
        voters = fields.PickledObjectField(blank=True, editable=False)
        created = models.DateTimeField(auto_now_add=True)
        is_published = models.BooleanField(default=False)
    
        def get_absolute_url(self):
            return "/video/%d" % self.id
    
        def views_num_plus(self):
            cursor = connection.cursor()
            cursor.execute('update soctv_video set views_num=views_num+1 where id=%d', [self.id])
            cursor.close()
    
        def update_comments_count(self):
            from threadedcomments.models import ThreadedComment as Comment
            self.comments_num = Comment.objects.filter(video=self).count()
            self.save()
            #cursor = connection.cursor()
            #cursor.execute('update soctv_video set comments_num = (select count(*) from soctv_comment where video_id = %s) where id = %s', [self.id, self.id])
            #cursor.close()
    
        def update_categories_counts(self):
            cursor = connection.cursor()
            cursor.execute('update soctv_category set num_items = (select count(*) from soctv_video_category where category_id = soctv_category.id)')
            cursor.close()
    
        def is_user_voted(self, uid):
            try:
                if self.voters[uid]:
                    return self.voters[uid]
            except Exception:
                return False
    
        def increment_view_count(self, token):
            import md5
            token = md5.new(token).hexdigest()
    
            if VideoView.objects.filter(uniquetoken=token).count() == 0:
                VideoView(uniquetoken = token, video = self).save()
    
        def view_count(self):
            return self.views_num + VideoView.objects.filter(video=self).count()
    
        def __unicode__(self):
            return unicode(self.title)
    
    2 回复  |  直到 9 年前
        1
  •  8
  •   kjagiello    15 年前

    问题可能是有些视频中的FK指向了一些不存在的东西。

    我也有同样的问题,这就是原因。

        2
  •  0
  •   Eyeball    9 年前

    确保您也登录到正确的帐户。 在我的情况下,我的帐户没有权限修改< 模型 gt;