Python で datatime の ミリ秒を変更したかったため以下のようなコードを実装した。
for elem in GistEntryPage.objects.all():
if elem.first_published_at.microsecond == 0:
elem.first_published_at.microsecond = 1
elem.save()
以下のエラーとなった。
AttributeError: attribute 'microsecond' of 'datetime.datetime' objects is not writable
datetime には、replace というメソッドが存在し、変更時には以下のように使用する。
for elem in GistEntryPage.objects.all():
if elem.first_published_at.microsecond == 0:
elem.first_published_at = elem.first_published_at.replace(microsecond = 1)
elem.save()
これで、ミリ秒を書き換えることができた。