Django管理员不显示所有实体 [英] Django admin does not show all entities
问题描述
然后我运行 python manage.py shell
,那里有code> Video.objects.all(),共有25个对象(使用 len
计数,并以
我没有找到任何经理或任何(也许我只是不知道在哪里找他们)
在管理页面的底部: 25个视频
,而只有16行。
然后我添加到VideoModelAdmin类 list_per_page = 10
,paginator显示三页,但只有前两个有任何视频,第3显示没有行。
以下是一些代码。
#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,请求,查询):
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()
返回实例
def update_comments_count(self,request,queryset):
在查询中的视频:
video.update_comments_count()
update_comments_count.short_description =Пересчитатькомментарии!
#later there
admin.site.register(Video,VideoModelAdmin)
#models.py
class视频(models.Model):
def make_upload_path(instance,filename):
return'video / thumbs /'+ generate_random_name(filename)
category = models.ManyToManyField ,limit_choices_to = {'is_published':1})
title = models.CharField(max_length = 128)
short_desc = models.CharField(max_length = 255)
long_desc = tinymce_models.HTMLField = 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 = mod els.BooleanField(default = False)
view_num = models.SmallIntegerField(default = 0,editable = False)
comments_num = models.SmallIntegerField(default = 0,editable = False)
rating = $ s $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
is_published = models.BooleanField(default = False)
def get_absolute_url(self):
return/ video /%d%self.id
def views_num_plus )
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.c ursor()
#cursor.execute('update soctv_video set comments_num =(select count(*)from soctv_comment where video_id =%s)其中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来自soctv_video_category的count(*),其中category_id = soctv_category.id)')
cursor.close()
def is_user_voted(self,uid):
try:
如果self.voters [uid]:
return self.voters [uid]
除了异常:
return False
def increment_view_count(self,token):
import md5
token = md5.new(token).hexdigest()
如果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)
问题可能是某些视频中的某些FK指向不存在。
我有同样的问题,这就是原因。
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)
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.
这篇关于Django管理员不显示所有实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!