Django 管理不显示所有实体

发布于 2024-08-18 09:22:47 字数 4150 浏览 4 评论 0原文

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

然后我运行python manage.py shell,然后出现Video.objects.all(),总共有25个对象(使用len对它们进行了计数)并使用 for 循环迭代它们)。

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

管理页面底部:25 个视频,而只有 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)

I've inherited an app created with Django. There is a problem with it: in admin interface, the page lists not all entities (videos), but some (16 of 25). I have no idea, what is this.

Then I run python manage.py shell, and there Video.objects.all(), there are all 25 objects (counted them using len and by iterating them with for loop).

I have found no managers or whatever (maybe I just don't know where to look for them).

On the bottom of admin page: 25 videos, while there are only 16 rows.

Then I add to VideoModelAdmin class list_per_page = 10, paginator show three pages, but only first two of them has any Videos, third shows no rows.

Here are some code.

# 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸢与 2024-08-25 09:22:47

问题可能是您的某些视频中的某些 FK 指向不存在的内容。

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

The problem can be that some FK in some of your videos points to something that does not exist.

I had the same problem and this was the reason.

靖瑶 2024-08-25 09:22:47

如果外键列中不存在该值,Django 将默默失败。
向列添加 null 和空白属性

null=True, blank=True

Django will silently fail if the value is not there in the foreign key column.
Add both null and blank attribute to the column

null=True, blank=True
笑饮青盏花 2024-08-25 09:22:47

确保您也登录到正确的帐户。
就我而言,我的帐户无权修改<模型>

Make sure that you are logged in to the correct account aswell.
In my case, My account did not have permissions to modify <Model>

他不在意 2024-08-25 09:22:47

我也遇到了同样的问题,也是通过 FK 引起的,管理面板中没有获取的值。
我在管理面板中添加/创建了该值,之后一切正常。

I had the same problem it was cause through a FK also, The taken value was not available in the admin panel.
I added/create the value in the admin panel and all thing works fine after that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文