1

I'm creating a simple project in Django to further my knowledge of it and Python, essentially just going to store bookmarks and tags, both will have views, and there will be 3 models:
Tag
Bookmark
Bookmark_Tag (relationships)

I read the "Projects vs Apps" panel in the django documentation, but I'm still unsure of how to lay it out, right now it's all within one Bookmark App, should there be a seperate app for Bookmarks and a seperate app for Tags, and if so, where does the model for the relationships live?

Thanks!

Dan LaManna
  • 3,431
  • 4
  • 23
  • 35

1 Answers1

5

No, you don't need a separate app for each. They're closely related, and it sounds like together they define your app, not separately. If later, you added another functionality to your site that used the same database and settings but didn't share much else with your current app, that would be another app in the same project.

See Django project models.py versus app models.py and Django: "projects" vs "apps" on this site as well as Django tips: laying out an application for some more guidelines.

If Bookmarks and Tags have a many-to-many relationship, and you need to add extra fields to that relationship (other than just the ids of the related objects) you can use a ManyToManyField and set the through table:

class Bookmark(models.Model):
    # whatever fields you need on Bookmark
    tags = models.ManyToManyField('Tag', through = 'BookmarkTag')

class Tag(models.Model):
    # whatever fields you need on Tag
    pass

class BookmarkTag(models.Model):
    bookmark = models.ForeignKey(Bookmark)
    tag = models.ForeignKey(Tag)
    # whatever additional fields you need on the relationship

See the Django docs on Extra fields on many-to-many relationships for more info.

Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238