Error: Invalid Date/Time Format – The value “0000-00-00 00:00:00.000000” matches the format but is invalid as a date/time in Django validation

`your
class EnrollmentModel(models.Model):
Semesters=[
(1, ‘One’),
(2, ‘Two’),
(3, ‘Three’),
(4, ‘Four’),
(5, ‘Five’),
(6, ‘Six’),
(7, ‘Seven’),
(8, ‘Eight’),
]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>payment_type=(
('Marchent','Marchent'),
('Bkash','Bkash'),
('Rocket','Rocket'),
('Nagad','Nagad'),
('Cash Hand','Cash Hand'),
('Apps','Apps'),
('Upay','Upay'),
)
enroll_status=(
('Pending','Pending'),
('Due','Due'),
('Due-Pending','Due-Pending'),
('Complete','Complete'),
('Refunded','Refunded'),
('Due-Paid','Due-Paid'),
('Customized-Paid','Customized-Paid'),
)
layer=(
('Full','Full'),
('Department','Department'),
('Non-Department','Non-Department'),
)
softmax_student_id = models.CharField(max_length=50, unique=True,null=True, blank=True)
student = models.ForeignKey(StudentModel, related_name='student_enroll', on_delete=models.CASCADE,null=True,db_index=True)
package = models.ForeignKey(PackageModel, related_name='package_enroll', on_delete=models.CASCADE,null=True,db_index=True)
plan = models.ForeignKey(PlanModel, related_name='plans', on_delete=models.CASCADE,null=True,db_index=True)
category = models.ForeignKey(CategoryModel, related_name='categories', on_delete=models.CASCADE,null=True,db_index=True)
employee = models.ForeignKey(User, related_name='employee_enroll', on_delete=models.CASCADE, null=True,db_index=True)
subject=models.ManyToManyField(DepartmentSubjectModel, related_name='dept_subject',blank=True)
semester = models.IntegerField(choices=Semesters, null=True, blank=True)
course_fee = models.IntegerField(default=0,null=True,blank=True)
payable_fee = models.IntegerField(default=0,blank=True,null=True)
due_payment_date = models.DateField(default=current_date, null=True, blank=True)
extra_amount = models.IntegerField(default=0,null=True, blank=True)
sponsored_amount = models.IntegerField(default=0,null=False, blank=True)
initial_paid = models.IntegerField(default=0,null=False, blank=True)
payment_type = models.CharField(max_length=100, choices=payment_type, blank=True, null=True)
payment_number = models.CharField(max_length=100, null=True, blank=True)
is_due_enrollment = models.BooleanField(default=False)
invoice_number = models.CharField(max_length=100, null=True, blank=True)
course_note = models.TextField(max_length=500, null=True, blank=True)
status = models.CharField(max_length=20, choices=enroll_status, default='Pending')
package_layer=models.CharField(max_length=100,null=True,blank=True,choices=layer)
created_at = models.DateTimeField( default=timezone.now,db_index=True,null=True, blank=True)
updated_at=models.DateTimeField(auto_now=True)
is_old_student= models.BooleanField( default=False, null=True, blank=True)
is_message_send= models.BooleanField( default=False,null=True, blank=True)
is_service_satisfied= models.BooleanField( default=False,null=True, blank=True)
</code>
<code>payment_type=( ('Marchent','Marchent'), ('Bkash','Bkash'), ('Rocket','Rocket'), ('Nagad','Nagad'), ('Cash Hand','Cash Hand'), ('Apps','Apps'), ('Upay','Upay'), ) enroll_status=( ('Pending','Pending'), ('Due','Due'), ('Due-Pending','Due-Pending'), ('Complete','Complete'), ('Refunded','Refunded'), ('Due-Paid','Due-Paid'), ('Customized-Paid','Customized-Paid'), ) layer=( ('Full','Full'), ('Department','Department'), ('Non-Department','Non-Department'), ) softmax_student_id = models.CharField(max_length=50, unique=True,null=True, blank=True) student = models.ForeignKey(StudentModel, related_name='student_enroll', on_delete=models.CASCADE,null=True,db_index=True) package = models.ForeignKey(PackageModel, related_name='package_enroll', on_delete=models.CASCADE,null=True,db_index=True) plan = models.ForeignKey(PlanModel, related_name='plans', on_delete=models.CASCADE,null=True,db_index=True) category = models.ForeignKey(CategoryModel, related_name='categories', on_delete=models.CASCADE,null=True,db_index=True) employee = models.ForeignKey(User, related_name='employee_enroll', on_delete=models.CASCADE, null=True,db_index=True) subject=models.ManyToManyField(DepartmentSubjectModel, related_name='dept_subject',blank=True) semester = models.IntegerField(choices=Semesters, null=True, blank=True) course_fee = models.IntegerField(default=0,null=True,blank=True) payable_fee = models.IntegerField(default=0,blank=True,null=True) due_payment_date = models.DateField(default=current_date, null=True, blank=True) extra_amount = models.IntegerField(default=0,null=True, blank=True) sponsored_amount = models.IntegerField(default=0,null=False, blank=True) initial_paid = models.IntegerField(default=0,null=False, blank=True) payment_type = models.CharField(max_length=100, choices=payment_type, blank=True, null=True) payment_number = models.CharField(max_length=100, null=True, blank=True) is_due_enrollment = models.BooleanField(default=False) invoice_number = models.CharField(max_length=100, null=True, blank=True) course_note = models.TextField(max_length=500, null=True, blank=True) status = models.CharField(max_length=20, choices=enroll_status, default='Pending') package_layer=models.CharField(max_length=100,null=True,blank=True,choices=layer) created_at = models.DateTimeField( default=timezone.now,db_index=True,null=True, blank=True) updated_at=models.DateTimeField(auto_now=True) is_old_student= models.BooleanField( default=False, null=True, blank=True) is_message_send= models.BooleanField( default=False,null=True, blank=True) is_service_satisfied= models.BooleanField( default=False,null=True, blank=True) </code>
payment_type=(
    ('Marchent','Marchent'),
    ('Bkash','Bkash'),
    ('Rocket','Rocket'),
    ('Nagad','Nagad'),
   
    ('Cash Hand','Cash Hand'),
    ('Apps','Apps'),
    ('Upay','Upay'),
)

enroll_status=(
    ('Pending','Pending'),
    ('Due','Due'),
    ('Due-Pending','Due-Pending'),
    ('Complete','Complete'),
    ('Refunded','Refunded'),
     ('Due-Paid','Due-Paid'),
    ('Customized-Paid','Customized-Paid'),
)
layer=(
    ('Full','Full'),
    ('Department','Department'),
    ('Non-Department','Non-Department'),
)

softmax_student_id = models.CharField(max_length=50, unique=True,null=True, blank=True)
student = models.ForeignKey(StudentModel, related_name='student_enroll', on_delete=models.CASCADE,null=True,db_index=True)
package = models.ForeignKey(PackageModel, related_name='package_enroll', on_delete=models.CASCADE,null=True,db_index=True)
plan = models.ForeignKey(PlanModel, related_name='plans', on_delete=models.CASCADE,null=True,db_index=True)
category = models.ForeignKey(CategoryModel, related_name='categories', on_delete=models.CASCADE,null=True,db_index=True)
employee = models.ForeignKey(User, related_name='employee_enroll', on_delete=models.CASCADE, null=True,db_index=True)
subject=models.ManyToManyField(DepartmentSubjectModel, related_name='dept_subject',blank=True)

semester = models.IntegerField(choices=Semesters, null=True, blank=True)
course_fee = models.IntegerField(default=0,null=True,blank=True)
payable_fee = models.IntegerField(default=0,blank=True,null=True)
due_payment_date = models.DateField(default=current_date, null=True, blank=True)
extra_amount = models.IntegerField(default=0,null=True, blank=True)
sponsored_amount = models.IntegerField(default=0,null=False, blank=True)
initial_paid = models.IntegerField(default=0,null=False, blank=True)

payment_type = models.CharField(max_length=100, choices=payment_type, blank=True, null=True)
payment_number = models.CharField(max_length=100, null=True, blank=True)
is_due_enrollment = models.BooleanField(default=False)
invoice_number = models.CharField(max_length=100, null=True, blank=True)
course_note = models.TextField(max_length=500, null=True, blank=True)
status = models.CharField(max_length=20, choices=enroll_status, default='Pending')
package_layer=models.CharField(max_length=100,null=True,blank=True,choices=layer)
created_at = models.DateTimeField( default=timezone.now,db_index=True,null=True, blank=True)
updated_at=models.DateTimeField(auto_now=True)
is_old_student= models.BooleanField( default=False, null=True, blank=True)
is_message_send= models.BooleanField( default=False,null=True, blank=True)
is_service_satisfied= models.BooleanField( default=False,null=True, blank=True)

class Update_EnrollmentSerializer(serializers.ModelSerializer):
# package = serializers.PrimaryKeyRelatedField(queryset=PackageModel.objects.all(), required=True)
# student = serializers.PrimaryKeyRelatedField(queryset=StudentModel.objects.all(), required=True)
subject = serializers.PrimaryKeyRelatedField(queryset=DepartmentSubjectModel.objects.all(), required=False, many=True)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>created_at = serializers.DateTimeField(
input_formats=[
'%Y-%m-%dT%H:%M:%S',
'%Y-%m-%dT%H:%M:%S.%f',
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%d',
'%Y-%m-%d %I:%M %p' # Handle "12:00 AM" case
],
required=False
)
class Meta:
model = EnrollmentModel
fields = '__all__'
# exclude=['created_at']
def update(self, instance, validated_data):
try:
subjects = validated_data.pop('subject',None)
created_at = validated_data.get('created_at', instance.created_at)
# Update created_at field if provided
if created_at:
instance.created_at = created_at
# Update other fields
for attr, value in validated_data.items():
setattr(instance, attr, value)
instance.save()
# Update Many-to-Many field (subjects)
if subjects is not None:
instance.subject.set(subjects)
except serializers.ValidationError as e:
# Handle validation errors specifically
raise serializers.ValidationError({"detail": str(e)})
except KeyError as e:
# Handle KeyError (missing field)
raise serializers.ValidationError({"detail": f"Missing field: {str(e)}"})
except Exception as e:
# Handle all other exceptions and log them
raise serializers.ValidationError({"detail": f"An unexpected error occurred: {str(e)}"})
return instance
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['student'] = instance.student.id if instance.student else None
representation['created_at'] = instance.created_at.strftime('%Y-%m-%d %H:%M:%S') if instance.created_at else timezone.now()
return representation
</code>
<code>created_at = serializers.DateTimeField( input_formats=[ '%Y-%m-%dT%H:%M:%S', '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d', '%Y-%m-%d %I:%M %p' # Handle "12:00 AM" case ], required=False ) class Meta: model = EnrollmentModel fields = '__all__' # exclude=['created_at'] def update(self, instance, validated_data): try: subjects = validated_data.pop('subject',None) created_at = validated_data.get('created_at', instance.created_at) # Update created_at field if provided if created_at: instance.created_at = created_at # Update other fields for attr, value in validated_data.items(): setattr(instance, attr, value) instance.save() # Update Many-to-Many field (subjects) if subjects is not None: instance.subject.set(subjects) except serializers.ValidationError as e: # Handle validation errors specifically raise serializers.ValidationError({"detail": str(e)}) except KeyError as e: # Handle KeyError (missing field) raise serializers.ValidationError({"detail": f"Missing field: {str(e)}"}) except Exception as e: # Handle all other exceptions and log them raise serializers.ValidationError({"detail": f"An unexpected error occurred: {str(e)}"}) return instance def to_representation(self, instance): representation = super().to_representation(instance) representation['student'] = instance.student.id if instance.student else None representation['created_at'] = instance.created_at.strftime('%Y-%m-%d %H:%M:%S') if instance.created_at else timezone.now() return representation </code>
created_at = serializers.DateTimeField(
    input_formats=[
        '%Y-%m-%dT%H:%M:%S', 
        '%Y-%m-%dT%H:%M:%S.%f', 
        '%Y-%m-%d %H:%M:%S', 
        '%Y-%m-%d',
        '%Y-%m-%d %I:%M %p'  # Handle "12:00 AM" case
    ],
    required=False 
)


class Meta:
    model = EnrollmentModel
    fields = '__all__'
    # exclude=['created_at']





def update(self, instance, validated_data):
    try:
        subjects = validated_data.pop('subject',None)
        created_at = validated_data.get('created_at', instance.created_at)

        # Update created_at field if provided
        if created_at:
            instance.created_at = created_at

        # Update other fields
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()

        # Update Many-to-Many field (subjects)
        if subjects is not None:
            instance.subject.set(subjects)

    except serializers.ValidationError as e:
        # Handle validation errors specifically
        raise serializers.ValidationError({"detail": str(e)})

    except KeyError as e:
        # Handle KeyError (missing field)
        raise serializers.ValidationError({"detail": f"Missing field: {str(e)}"})

    except Exception as e:
        # Handle all other exceptions and log them
        raise serializers.ValidationError({"detail": f"An unexpected error occurred: {str(e)}"})

    return instance

def to_representation(self, instance):
    representation = super().to_representation(instance)
    representation['student'] = instance.student.id if instance.student else None
    representation['created_at'] = instance.created_at.strftime('%Y-%m-%d %H:%M:%S') if instance.created_at else timezone.now()
    return representation

text`

Error:
{
“detail”: “An unexpected error occurred: [‘“0000-00-00 00:00:00.000000” value has the correct format (YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time.’]”
}

In database, created_at=’2024-10-28 00:00:00.000000′, I dont understand why raise this error . please help

New contributor

Md. Rahim Uddin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật