Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin does not show all entities

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)
like image 501
Valentin Golev Avatar asked Jan 18 '10 11:01

Valentin Golev


People also ask

How do I restrict access to parts of Django admin?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

What does admin do in Django?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

Does Django have admin panel?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


2 Answers

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.

like image 140
kjagiello Avatar answered Sep 29 '22 00:09

kjagiello


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
like image 37
Anand Tripathi Avatar answered Sep 29 '22 01:09

Anand Tripathi