`your
class EnrollmentModel(models.Model):
Semesters=[
(1, ‘One’),
(2, ‘Two’),
(3, ‘Three’),
(4, ‘Four’),
(5, ‘Five’),
(6, ‘Six’),
(7, ‘Seven’),
(8, ‘Eight’),
]
('Cash Hand','Cash Hand'),
('Due-Pending','Due-Pending'),
('Customized-Paid','Customized-Paid'),
('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)
</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)
<code>created_at = serializers.DateTimeField(
'%Y-%m-%d %I:%M %p' # Handle "12:00 AM" case
def update(self, instance, validated_data):
subjects = validated_data.pop('subject',None)
created_at = validated_data.get('created_at', instance.created_at)
# Update created_at field if provided
instance.created_at = created_at
for attr, value in validated_data.items():
setattr(instance, attr, value)
# Update Many-to-Many field (subjects)
instance.subject.set(subjects)
except serializers.ValidationError as e:
# Handle validation errors specifically
raise serializers.ValidationError({"detail": str(e)})
# Handle KeyError (missing field)
raise serializers.ValidationError({"detail": f"Missing field: {str(e)}"})
# Handle all other exceptions and log them
raise serializers.ValidationError({"detail": f"An unexpected error occurred: {str(e)}"})
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()
<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