Reputation: 1775
I created a model which name is Post and placed in this path : MyProject/Postapp/models look like this:
class Post(models.Model):
user = models.ForeignKey(User, related_name="posts",
on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
message_html = models.TextField(editable=False)
group = models.ForeignKey(Group, related_name="posts",null=True,
blank=True, on_delete=models.CASCADE)
def __str__(self):
return self.message
def save(self, *args, **kwargs):
self.message_html = misaka.html(self.message)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse(
"posts:single",
kwargs={
"username": self.user.username,
"pk": self.pk
}
)
class Meta:
ordering = ["-created_at"]
unique_together = ["user", "message"]
So in the following, I want to add a comment, ... to each Post which created by Users. Should I start a new app (Commentapp, ...) or create models in the current path and add some fields in the Post model?
Upvotes: 2
Views: 1230
Reputation: 77912
The answer is the same as for any "module" in any language (at least in any language that has some concept of modularity): you want strong cohesion (features of a module should be closely related) and low coupling (your module should have as few dependencies as possible, and you specially don't want cyclic dependencies).
In your case, Comments
are obviously closely related to Posts
so it makes perfect sense to keep them together (strong cohesion), while splitting them into different aps would actually add dependencies (and possibly cyclic ones) so it would increase coupling.
NB: unless you want a generic comments
app that can work with any other model of course (in which case one already exists), but from experience I would not get into this kind of premature generalization - most often than not you end up not using your "generic" model with anything else so all it buys you is only extra complexity (and quite some of it) for no real benefit. The mere point that the existing contrib.comments
app was finally removed from the django trunk is a good enough sign that this was not a good candidate for a generic reusable app (as far as I'm concerned I used it once on a project, then dumped it and reimplement my own model which worked the way I needed with much less code and much better performances).
Upvotes: 1