TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Fat Models – A Django Code Organization Strategy

52 点作者 bit2pixel超过 11 年前

11 条评论

caseydurfee超过 11 年前
Fat models are slightly better than having that crud in your views, but they&#x27;re still an antipattern. Models should describe the relationships between entities in your business, not be the junk drawer for stuff that doesn&#x27;t go anywhere else.<p>Fat models are already a code smell, but passing the request object to your model should really set off alarm bells. You&#x27;ve now made it extremely messy to use your models outside of a web context, making it impossible to unit test them, and pretty much wiped out the purpose of the controller as an encapsulation layer.<p>Domain-driven design (DDD) provides a lot of patterns that help to keep from either having model code in your views, or these types of junk drawer models.<p>Basically, DDD as it applies to MVC webapps is: If it&#x27;s a complex query of some sort, create a query repository that returns model objects rather than a &quot;fat&quot; static method on the model itself. If it&#x27;s a business transaction, create logic objects that encapsulate the biz logic, and service objects that applies the logic to the models. The controller serves as the broker between requests, services, and entities. You end up with much more testable and reusable code this way.
评论 #7141232 未加载
评论 #7142864 未加载
评论 #7142572 未加载
unoti超过 11 年前
I write my code as if there will be multiple user interfaces. Usually there are not multiple user interfaces, but thinking as if there will be has a positive impact on the code. Thinking as if there will be multiple user interfaces means putting as little code as possible into the views, since there will be more than one. Some of those user interfaces might not even have Django views and templates, such as a service exposed via Zero MQ. This naturally leads me to putting as much logic as I can either into the models, or sometimes into other modules which in turn use the models. About the only code that goes into views then is the glue that looks things up and gets things ready to be passed into a model.
edwinnathaniel超过 11 年前
I can only shake my head in disbelief when I heard Rails or Django developers touting their framework and saw their actual codebase (ever see FatCRM codebase back in the days? it has been improved significantly these days, but it used to be ... hard to read).<p>I even shake my head faster when I read the split in opinions in Rails world: the Rails DHH and the DCI&#x2F;Service Rails.<p>Moreover, I keep shaking my head when the Django and Rails community voiced their complain over their so-called bloated framework just because they want an API service.<p>Last but not least, I shake my head again whenever I heard that Django and Rails is not like NodeJS.<p>C&#x27;mon guys... you guys speak a lot of bad things about JAVA yet you&#x27;ve become Java Enterprise Edition.<p>I suppose JavaEE (especially 6 and 7) does work better in terms of writing maintainable code.<p>Stuff like Service pattern, Repository pattern, Data mapping pattern, DTO, Value Objects, Mocking are pretty much &quot;common&quot; in most Java projects I&#x27;ve seen or worked on.<p>I felt that no matter how cool Rails and Django are, when it comes to medium-to-large codebase, Rails&#x2F;Django are definitely either following the patterns that pretty much common in JavaEE or may not be so comfortable to use anymore.
Daishiman超过 11 年前
I honestly think this guy doesn&#x27;t get it.<p>The proper way to separate concerns in Django is through applications, which have their own URL routes, models, and views (although they can invoke any other thing in another app).<p>Through Class-based views you can define base views for common behavior for templates and basic logic, and the built-in views are usually good enough to contain most patterns and easy enough to customize for any other use case you might think of.<p>A good rule of thumb is that an application should generally have less than a dozen views (I&#x27;d say 5 or 6 is enough already). Since each app has its own forms and models file, adding utility methods in an extra file contained within an app is reasonable.<p>I&#x27;ve used this to manage Django apps with over 50.000 lines of custome code (not counting several dozen extra apps and admin sites with their own customization) and it has never been a problem.<p>Fat models are just a massive problem. If Python supported extension methods then perhaps you could load your own extensions when performing an action, but a proper design of forms and views that aims for maximum simplity has never failed me.
lgunsch超过 11 年前
I would stick with no classes being &quot;fat&quot;. Breaking up fat classes into many smaller classes leads to code being really easy to understand, test, and develop. I tend to stick with the Single-Responsibility-Principle and classes do not become unmanageable. Some people argue that many small classes leads to complex code, but I have not found that to be true in any case I have come across.
评论 #7141334 未加载
评论 #7141360 未加载
salsakran超过 11 年前
Not sure I agree with fat models being a &quot;rarely seen, alternative code organization strategy&quot;.<p>It&#x27;s a natural state which most django codebases tend towards. There&#x27;s a natural Views use ModelForms use Models data flow which leads to bloat somewhere in that chain as you add code. Fat models are easiest to test and the natural place to put most things, but eventually massive models get unwieldly and you start putting things into helpers. The core thing the author seems to have conflate util functions that have access to a request and helpers that have no connection to the request&#x2F;response cycle.
评论 #7141450 未加载
Demiurge超过 11 年前
&quot;Fat&quot; abstract models is the way to go if you have logic that operates on the data. It&#x27;s not even fat. There were fields assigned values in the example. That is exactly what model methods should do.<p>&quot;Fat&quot; abstract forms is the way to go if your forms have crispy layouts, placeholder initializers, or other stuff that that directly has to do with forms.<p>Views can be easily split into categorical files and shouldn&#x27;t contain much logic other than bringing models and presentation together.<p>Sounds like thinking about what you&#x27;re doing is the way to go to not have code get out of hand.
mrfusion超过 11 年前
Maybe you guys can help me apply this to an actual Django project I&#x27;m working on.<p>So I made an &quot;Order&quot; model to track orders. But now I have to add a lot of logic regarding things such as what can be ordered together, and which users can order what.<p>Where you keep that logic? It seems to make sense to me to put it right in the model, no?
评论 #7144991 未加载
davvid超过 11 年前
There is no &quot;one right way&quot;, but when thinking about code organization and architecture I find inspiration from the &quot;clean architecture&quot;[1] and similar approaches. The dependency rule can really turn your world upside down!<p>[1] <a href="http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html" rel="nofollow">http:&#x2F;&#x2F;blog.8thlight.com&#x2F;uncle-bob&#x2F;2012&#x2F;08&#x2F;13&#x2F;the-clean-arch...</a>
rartichoke超过 11 年前
Does anyone else feel weird knowing that when you call that model method it may or may not call out to a background worker task?<p>Why not just throw all of your celery tasks that are related to an app inside of a worker.py file or something else?<p>If you import from service.py or worker.py then it becomes obvious the thing you&#x27;re doing is happening in the background.
mariocesar超过 11 年前
Is this article pointing out the most common way of organizing code in Django as an advice?<p>I&#x27;m such a failure at marketing ... sigh