Tutorial on Django Models - bounswe/bounswe2024group11 GitHub Wiki
- You can define a field such as text or pub_date.
class Post(models.Model):
text = models.CharField(max_length=200)
pub_date = models.DateTimeField('Publication Date', auto_now=True)
def get_readable_date(self):
return self.pub_date.strftime("%B %d, %Y")
- you can create a method to use in your serializer as source such as get_readable_date() as above. By setting read only to True, user is not allowed to edit it. Use this as source in serializer:
class PostSerializer(serializers.ModelSerializer):
pub_date = serializers.CharField(source = 'get_readable_date', read_only = True)
class Meta:
model = Post
fields = ["pub_date"]