Implement a feed stream in Django
I've a Django application that uses a Product
model and a Comment
model.
A
User
can add aProduct
to its favorite products list.A
User
can leave a开发者_StackOverflow中文版Comment
to aProduct
.
I would implement a news feed in the home of my application, something like Facebook News feed.
Something like this:
- user_1 just comments product_3: "this is beautiful!"
- user_1 just added product_3 to its list
- user_4 just added product_2 to its list
- user_4 just added product_3 to its list
- user_2 just comments product_1: "recommended!"
- user_4 just added product_1
- etc.
So it's a feed with various type of sentences.
Have you ideas to implement something like that in a good way?
- Django ORM supports polymorphic associations. You can have a "base" entity like "Event" and concrete events like "UserEvent", "ProductEvent" and so forth. That's quite easy. Look here: https://docs.djangoproject.com/en/dev/topics/db/models/ search for "model inheritance"
- Take a look at content types: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
- You can probably just save "rendered" text to the DB.
I would suggest to create a new model (let's say UserActions) in which you will trace all desired actions by adding an item on each of your others models "save" action. Then you can easily build a view for this model and generate a feed with all the actions in chronological order.
精彩评论