我要加DurationField
用于管理站点的小部件,并希望持续时间字段的小部件用于输入。
问题陈述
在下面 PromoCode
类有DurationField
即 duration
.但是在管理中它显示 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
字段不是人类可读的格式。预期行为
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/