The Other Guy
The Other Guy

Reputation: 576

App as foreign key

So , i have a django project with 2 apps

├───Blog
    post
    comment

└───User
    profile

I need to give the users the possbility to create one or more blogs within the same project, but i don't see how i can treat an app like a model.

The only solution i figured out , is to add a foreign key to user id for every model withn the blog app.but is there a better way ?

Upvotes: 0

Views: 65

Answers (1)

Leandro
Leandro

Reputation: 2247

I think you should do that:

# blog/models.py

class Blog(Model):
    owner = ForeignKey(User, related_name="blogs")
    name = Charfield()

class Post(Model):
    blog = ForeignKey(Blog, related_name="posts")
    #Other fields ...

class Comment(Model):
    post = ForeignKey(Post, related_name="comments")
    #Other fields ...

Upvotes: 1

Related Questions