Reputation: 519
I need to get the time since a post object was created. While surfing the internet I have found the following implementation and it gives me the following error
Adding @property
to models post object
class Post(models.Model):
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
@property
def timesince(self):
return timesince.timesince(self.timestamp)
The PostSerializer
class PostSerializer(serializers.ModelSerializer):
timesince = serializers.DateTimeField()
class Meta:
model = Post
fields('__all__')
I get the following AttributeError
Got AttributeError when attempting to get a value for field `timesince` on serializer `PostSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Post` instance.
Original exception text was: 'NoneType' object has no attribute 'year'.
Upvotes: 0
Views: 421
Reputation: 1860
the way you define the fields is wrong; the correct syntax is
class Meta:
model = Post
fields = '__all__'
Upvotes: 2