7

Djangoモデルに、年、月、日がすべてオプションである日付フィールドを持たせたいです。値の例は、と同じくらい一般的である2008か、または同じくらい具体的である可能性がありますMay 10, 2008DateFieldこのように動作するようにを定義することは可能ですか?または、このように年/月/日を個別の整数として定義する方がよいでしょうか?

model Book(models.Model):
    publication_year = models.IntegerField(blank=True, null=True)
    publication_month = models.IntegerField(blank=True, null=True)
    publication_day = models.IntegerField(blank=True, null=True)
4

2 に答える 2

8

https://github.com/dracos/django-date-extensionsのdjango-date-extensions (私は、まさにこの目的のために)には、月や日がない可能性のある日付を処理するためのExplicitDateオブジェクトが含まれています。

于 2012-07-05T23:07:43.927 に答える
2

DjangoクラスのDateTimeFieldは、Pythonのdatetime.datetimeクラスに基づいており、年、月、日は必須です。

datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

あなたの代わりに、3つの整数フィールドを定義する代わりに、Django DateTimeFieldと、年のみまたは日付全体を取得する必要があるかどうかを通知するブール値を使用します。

于 2012-07-05T23:07:15.097 に答える