Hi Hi.
I have a model
class Comment(models.Model):
""" Comments model """
cmnt_author = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name='cmnt_author',
verbose_name='Comment author',
null=True,
blank=True,
on_delete=models.CASCADE,
)
cmnt_txt = models.TextField(
_('Text'),
blank=True,
max_length=480,
)
date_added = models.DateTimeField(
_('Date added'),
default=datetime.datetime.now
)
# Relationship with many models through ForeignKey
content_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
null=True,
blank=True,
verbose_name='Content type',
)
object_id = models.PositiveIntegerField(
null=True,
verbose_name='Object ID',
)
content_object = GenericForeignKey('content_type', 'object_id')
And I have a serializer. I use this serializer to get comments.
class CommentSerializer(serializers.ModelSerializer):
date_added = serializers.DateTimeField(format='%d %b %Y')
class Meta:
model = Comment
fields = ('id', 'cmnt_avtr', 'author', 'cmnt_txt', 'date_added')
Question:
1) How can I use the same serializer to add comments? But to create a comment, I use fewer fields.
2) How to serialize adding comments when I use Generic Foreign Key
In particular, to add, I use these fields:
fields = ('cmnt_author', 'cmnt_txt', 'content_type', 'object_id',)