views:

87

answers:

2
+1  Q: 

Design question?

I am building music app, where user can do several tasks including but not limited to listening song, like song, recommend song to a friend and extra. currently I have this model:

class Activity(models.Model):
    activity = models.TextField()
    user = models.ForeignKey(User)
    date = models.DateTimeField(auto_now=True)

so far I thought about two solutions. 1. saving a string to database. e.g "you listened song xyz". 2. create a dictionary about the activity and save to the database using pickle or json.

e.g.

dict_ = {"activity_type":"listening", "song":song_obj}

I am leaning to the second implementation, but not quite sure.

so what do you think about those two methods? do you know better way to achieve the goal?

A: 

Looks like all you need is an enumeration to match a table in your database.

So in your UserActivity table, you will have userid, activityid, songid, datetime etc etc

CodeToGlory
though about that too, but how about if user friends another user, or creates playlist. I will end up having so many nulls.
Mohamed
+1 imagine trying to code/filter/report on all the different activity types by parsing strings. Ugh. You definitely want short, preferably integer, and definitely unambiguous identifiers for the activities. Build the human-readable descriptions on demand to suit the context where they are displayed.
Jarret Hardie
This will be a cross reference table, so you will only have users that have songs. You can even extend this by having a playlistid and then you can take the songid out to some other table. There are lot of different ways to do this. Just do no use strings.
CodeToGlory
A: 

What about GenericForeignKey? You could have userid, activityid and content_object on your UserActivity where content_object is a song, an user or something completely different.

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
rebus