开发者

Implement a feed stream in Django

开发者 https://www.devze.com 2023-03-22 15:04 出处:网络
I\'ve a Django application that uses a Product model and a Comment model. A User can add a Product to its favorite products list.

I've a Django application that uses a Product model and a Comment model.

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?


  1. 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"
  2. Take a look at content types: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消