[Fixed]-How to setup many to many one to many and many to one relationship in django

1๐Ÿ‘

โœ…

You have two options:

  1. Change Author and Post position, thus Page can see Post definition.
  2. Use Lazy call as: author= models.ForeignKey("Author")

In this way, Django will wait until all models load then resolve the dependencies.

๐Ÿ‘คAli Nikneshan

0๐Ÿ‘

Try model structure syntax:

class Author(models.Model):
    name = models.CharField(max_length=150)
    email = models.EmailField(blank=True)
    bio = models.TextField()

    def __str__(self):
        return self.author

class Post(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=200)
    body = models.TextField()
    created_at = models.DateTimeField('created date', auto_now_add=True, auto_now=False)
    updated_at = models.DateTimeField('updated date', auto_now_add=False, auto_now=True)

    def __str__(self):
        return self.title

class Category(models.Model):
    cat_name = models.CharField(max_length=200)
    post = models.ManyToManyField(Post)

    def __str__(self):
        return self.cat_name

class Tag(models.Model):
    tag_name = models.CharField(max_length=200)
    post = models.ManyToManyField(Post)

    def __str__(self):
        return self.tag_name

This will solve your three requirements:
1.) Author can have many posts because I set the Post Model into one-to-many field
2.) Categories can have many posts vice versa (This is already set according to your code)
3.) Tag can have many posts vice versa (Very similar to your already made Category Model where I made the field many-to-many between the two)

The reason why you got an error is because you made your author to post relation to many-to-one (many authors in one post) according to your model structure

Now to test it simply migrate all these changes and quickly test it in your admin page

0๐Ÿ‘

  1. author can have many posts, A post can have only single author

Post model should have a ForeignKey pointing to Author.

class Post(...):
    author = models.ForeignKey('Author')
  1. categories can have many posts, A post can have many categories

Post model should have a ManyToManyField pointing to Category.

class Post(...):
    category = models.ManyToManyField('Category')
  1. a tag can have many posts a post can have many tags

Same as number 2.

๐Ÿ‘คxyres

Leave a comment