开发者

Fetching specific Goals for User code through complicated logic

I want to fetch a specific Goal through complicated logic for a certain User. Is this code supposed to go in the View开发者_StackOverflow社区 or the Model?

If it is supposed to go in the Model, how would it be implemented, since User Model is written by Django and I can't add methods?

I realize this is not a specific error question but it seems to be that should be a no-brainer to you, gurus and consequently should not yield vague answer.

Thanx


You could put it in the model or the view, or even do processing externally using a job/task queue like celery. In regards to the model vs. view question, I tend to prefer to put the logic into a view, especially if I need to process data from multiple models collectively. If you implement your logic as a method on your model, you can call it from a view or anywhere you might want. You could also implement caching very easily and store results on the model. Consider using AJAX to pull in the results asynchronously. You should also check the docs for info on storing additional info about a user.

Store logic in view

  • Saves from needlessly complicating model
  • Might be a better approach if you need to interact with multiple models
  • Easy to implement and maintain
  • Not as flexible, can't be reused everywhere

Store logic on model

  • Makes it really easy to use all over your project, in views and/or scripts
  • Easy to do caching on the model
  • Potentially easy to customize the logic for a wide variety of usages through inheritance
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜