This is my model
class modelTest(models.Model):
created_at = models.DateTimeField(auto_now_add=True,blank=True,null=True)
updated_at = models.DateTimeField(auto_now=True,blank=True,null=True)
imageA = models.ImageField(upload_to='images/', default='',null=True)
This is the related serializer
class Serializer_Job_TX(serializers.ModelSerializer):
class Meta:
model = modelTest
fields = [
'created_at',
'updated_at',
'imageA',
]
How can i make sure that when serialization happens for date time fields it only outputs the date and not the time ? any suggestions
Override created_at and updated_at properties in the serializer. So that you can define your own format using format option. And don't forget to add read_only=True. Else you will get an error message("these fields are required") on adding data to the model.
class Serializer_Job_TX(serializers.ModelSerializer):
created_at = serializers.DateTimeField(read_only=True, format="%Y-%m-%d")
updated_at = serializers.DateTimeField(read_only=True, format="%Y-%m-%d")
class Meta:
model = modelTest
fields = [
'created_at',
'updated_at',
'imageA',
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With