django - Django 中是否有可用于 `DurationField` 的小部件?

标签 django django-admin django-widget

我要加DurationField用于管理站点的小部件,并希望持续时间字段的小部件用于输入。
问题陈述
在下面 PromoCode类有DurationFieldduration .但是在管理中它显示 TextInput作为输入。

class PromoCode(models.Model):
    """
    Promo Code model to maintain offers
    """
    code = models.CharField(_("Code"), max_length=60, help_text="Promo code")
    # Validations and constraints for promo code
    start_date = models.DateField(_("Start date"), null=True, blank=True, help_text="Start date of promo code offer")
    end_date = models.DateField(_("End date"), null=True, blank=True, help_text="End date of promo code offer")
    duration = models.DurationField(_("Duration"), null=True, blank=True, help_text="Validity period of promo code")
    ...
    ...

admin.py
class PromoCodeAdmin(admin.ModelAdmin):
    """
    Model admin for promocodes
    """
    list_display = ('code', 'description', 'start_date', 'end_date',)
    fields = (
    'code', 'description', 'discount', 'start_date', 'end_date', 'duration', 'sitewide_countdown', 'user_countdown',
    'coin_currencies', 'fiat_currencies',)

下图仅供引用,duration字段不是人类可读的格式。
Reference image
预期行为

What is the best way to add DurationField 's widget into admin form to make it easy to read and edit? Currently it is quite hard for admin to add time duration.

最佳答案

您可以使用 @pre_save在管理员中并将持续时间转换为您想要的可读格式。

关于django - Django 中是否有可用于 `DurationField` 的小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55065065/

相关文章:

预填充数据库的 Django 测试脚本

python - Django Hello World 没有显示

django - list_display - bool 图标不适用于 BooleanField

Django 管理员 : how to sort column by custom method

python - 向 Django 外键添加多个值

django - 过滤多对多多选字段

mysql - django-balanced - 数据库错误

无法使用 IPV6 地址访问 Django 项目

python - Django 表单字段小部件变得隐藏

django - 自定义 Django 管理员更改表单外键以包含查看记录