views:

268

answers:

1

Hello!

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)
+4  A: 

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.

Balon
oh. thanks. I see. can I do something except of changing FK in console by hands?
valya
thanks, I've for i in Video.objects.all():... i.author = adm... i.save()saved me :)
valya