Django admin - 为每一行创建一个编辑按钮

时间:2017-02-22 18:40:48

标签: python django button django-admin

我必须创建一个django管理按钮,使我的表格的每一行都可编辑(点击按钮后)。然后保存已编辑的行而不重定向到任何页面。

要100%清除:点击“修改”时,该行中的所有字段都是可编辑的。 每行1个编辑按钮。

django管理员图片: https://gyazo.com/def02a6ed68ddc2e777c0daaf26a0c5d

我的admin.py

from django.contrib import admin
from .models import Remedio
from django.contrib.admin import AdminSite


class MyAdminSite(AdminSite):
    site_header = 'Farmacia'

class RemedioAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['descricao']}),
        (None, {'fields': ['data_de_fabricacao']}),
        (None, {'fields': ['data_de_validade']}),
        (None, {'fields': ['manipulado']}),
        (None, {'fields': ['maioridade']}),
        (None, {'fields': ['preco']}),
        (None, {'fields': ['faca_editar']}),

    ]

    list_display = ('descricao', 'data_de_fabricacao', 'data_de_validade', 'preco', 'maioridade', 'manipulado', 'faca_editar')
    list_filter = ['preco']
    search_fields = ['descricao', 'preco']
    list_editable = ['preco']
    list_display_links = ['data_de_fabricacao']
    # readonly_fields = ['preco']


    def make_price_zero(self, request, queryset):
        queryset.update(preco=0.00)
    make_price_zero.short_description = "Marque os remedios que queira zerar o preco"

    actions = ['make_price_zero']

admin.site.register(Remedio, RemedioAdmin)
admin_site = MyAdminSite(name='farmacia_vitu')

my models.py

from __future__ import unicode_literals
from django.db import models

class Remedio(models.Model):
    descricao = models.CharField(max_length=200)
    data_de_fabricacao = models.DateTimeField()
    data_de_validade = models.DateTimeField()
    imagem = models.ImageField()
    maioridade = models.BooleanField()
    manipulado = models.BooleanField()
    preco = models.DecimalField(decimal_places=2, max_digits=8)
    faca_editar = models.URLField()


    def __str__(self):
        return self.descricao

0 个答案:

没有答案