Django Admin面板 - 在'list_display'小部件中添加一个custome字段,该小部件不是模型类的属性(不改变models.py)

时间:2017-08-11 12:51:01

标签: python django django-models django-forms django-admin

我想在'genre'类中添加一个名为'count'的字段,该字段可以显示该类型的电影数量。 的 models.py

    class genre(models.Model):
        genre_name = models.CharField(max_length=20, verbose_name='Genre')

    def __str__ (self):
        return(self.genre_name)

    class movie(models.Model):

         movie_name = models.CharField(max_length=50,  verbose_name='Movie Name')
         movie_rating = models.IntegerField(default=1)
         movie_genres = models.ManyToManyField(genre, verbose_name='Genre(s)')
         added_by = models.CharField(max_length=20, blank=True)
         email = models.EmailField(blank=True)
         Date = models.DateTimeField(default=datetime.now)

    def __str__ (self):
        return(self.movie_name)

admin.py

    from __future__ import unicode_literals
    from django.contrib import admin
    from . models import movie, genre
    from django import forms

    class movieAdmin(admin.ModelAdmin):

        list_display=('movie_name', 'movie_rating','Date')
        search_fields=('movie_name',)
        filter_horizontal=('movie_genres',)
        list_filter=('Date',)
        date_hierarchy='Date'
        ordering=('-movie_name',)
        fields=('movie_name','movie_rating')

    admin.site.register(movie, movieAdmin)
    admin.site.register(genre)

我想在list_display中添加 count 字段

0 个答案:

没有答案