I am building an activity model, somewhat similar to this package
<https://github.com/justquick/django-activity-stream>. It has an actor,
verb and the target.

class Activity(models.Model):
    actor_type = models.ForeignKey(ContentType,
related_name='actor_type_activities')
    actor_id = models.PositiveIntegerField()
    actor = GenericForeignKey('actor_type', 'actor_id')
    verb = models.CharField(max_length=10)
    target_type = models.ForeignKey(ContentType,
related_name='target_type_activities')
    target_id = models.PositiveIntegerField()
    target = GenericForeignKey('target_type', 'target_id')
    pub_date = models.DateTimeField(default=timezone.now)


Now whenever a new object of whichever models (Tender, Job and News) is
created, a new Activity object is created, with the target being the
objects of any of these three models.

eg. user (actor) published (verb) title (target)

class Tender(models.Model):
    title = models.CharField(max_length=256)
    description = models.TextField()

class Job(models.Model):
    title = models.CharField(max_length=256)
    qualification = models.CharField(max_length=256)

class News(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    title = models.CharField(max_length=150)

To get this data I am making an API which will get me the required json
data. I am using django-rest-framework
<http://www.django-rest-framework.org/> for this and very new with it.

class ActorSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email')

class ActivitySerializer(serializers.HyperlinkedModelSerializer):
    actor = ActorSerializer()
    class Meta:
        model = Activity
        fields = ('url', 'actor', 'verb', 'pub_date')

In the above serializers, I knew that *actor *will be the *User*. And so I
used the User model for the *ActorSerializer class*. But as for the *target*,
it can be any of these three models (News/Job/Tender).

How can I make a serializer (eg. TargetSerialier class) for the ContentType
object so that I can use the target in the ActivitySerializer class field?

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWuRvs6mJFwyMfzFXXVxjpF%3D_L6jcWJiH-Dir5pZZ5RX0Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to