1๐
You have two options:
- Change Author and Post position, thus Page can see Post definition.
- Use Lazy call as:
author= models.ForeignKey("Author")
In this way, Django will wait until all models load then resolve the dependencies.
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
- Django session language doesn't apply to the first page the user lands on
- Django templates json loops
- DJango 1.8 use completely different CSS on child template
0๐
- 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')
- 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')
- a tag can have many posts a post can have many tags
Same as number 2.
- Django / WagtailCMS โ get attribute (eg body text) of child page using get_context
- How to send a variable from javascript to View in django
- Downloadable File from Django Management Command
- Could not parse the remainder: '((records_list.1.key.5)' from '((records_list.1.key.5)'