gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:42:30 24 4
gpt4 key购买 nike

我要加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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com