Py学习  »  Django

如何在django admin中添加自定义按钮以运行django management命令

ABHI KHANDELWAL • 4 年前 • 767 次点击  

我必须在我的django管理页面的一个模型中添加一个更新按钮,match-action是django管理上的一个更新按钮,match-status是我的管理命令,这是我的代码,实际上我想,当我单击更新按钮时,我想使用manageme运行一个名为goalServeService的服务。NT命令

管理.py`

@admin.register(Match)
class MatchAdmin(admin.ModelAdmin):
    list_display = ['id', 'start_time', "home_club", 'away_club', 'created_on', 'status', 'lookup_id', 'timer', 'match_actions']
    search_fields = ['id', 'home_club__name', 'away_club__name']
    list_filter = ['status', 'season', 'start_time']
    inlines = [ MatchLookupInline, MatchEventInline, ]

    def lookup_id(self, obj):
        if obj.matchlookup_set.all().exists():
            return obj.matchlookup_set.get().source_match_id
        else:
            return None
    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
        url(
            r'^(?P<match_id>.+)/update/$',
            self.admin_site.admin_view(self.match_update),
            name='match-update',
        ),

        ]
        return custom_urls + urls

    def match_actions(self, obj):
        print (obj.id)
        return format_html(
            '<a class="button" href="{}">Update</a>',
            reverse('admin:match-update', args=(obj.id,)),

        )

    def match_update(self, request, match_id, *args):
        from django.core.management import call_command
        call_command("match_status", match_id)

核心/管理/命令/匹配状态.py

from django.utils import timezone
from django.core.management.base import BaseCommand, CommandError
from core.models import UserTeam
from ourapp.models import Match


class Command(BaseCommand):
    help = 'Update  Match Status'

    def add_arguments(self, parser):
        parser.add_argument('match_id', nargs='+', type=int)

    def handle(self, *args, **options):
        # NOT_STARTED = 0
        # HF = 1
        # FT = 2
        # IN_PLAY = 3
        # FINISH = 4
        match = Match.objects.filter(id=options.get('match_id'))
        self.stdout.write(self.style.NOTICE("Match competition Name %s Match Found" % match.competition.name))
        current_time = timezone.now()


        try:
            import time

            self.stdout.write(self.style.NOTICE("Match competition Name %s Match Found" % match.competition.name if match.competition else "N/A"))

            match = Match.objects.filter(id=options.get('match_id'))
            time.sleep(5)
            from ourapp.services import goalserveService
            try:                        
                 goalserveService.run_match_events(match.pk, 2, update_df=False, update_round=False)
                 time.sleep(5)
            except:
                pass

            from django.core.management import call_command
            call_command("update_dfc_rank", 1)
        except Match.DoesNotExist:
            raise CommandError('League "%s" does not exist' % match)
        self.stdout.write(self.style.SUCCESS('Successfully closed league "%s"' % match))

但它显示了一个错误

my admin page

when I click update button

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38518
 
767 次点击  
文章 [ 1 ]  |  最新文章 4 年前
deHaar Darrell Teague
Reply   •   1 楼
deHaar Darrell Teague    5 年前

任何时候你看到 .objects.filter 在您的代码中,您将返回 QuerySet . 查询集 S可以解释为“列表”。对于起动装置,更换 .filter 具有 .get 解决当前的错误(但我不能保证您以后不会遇到新的错误)。