-2

Hi have been working with Django and i want to have relations bettwen model i have the following structure

enter image description here

on posts/models.py

from django.db import models
class Post(models.Model):

(SKIP ATTRIBUTES)

and then on comments/model.py
from django.db import models
from posts.models import Post

class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')

In a nutshell im trying to import posts model into comment model and i get the error that cannot import name 'Post' from 'posts.models , how should import posts model to avoid this issue ?

from posts.models import Post ImportError: cannot import name 'Post' from 'posts.models

2 Answers2

2

If you are also importing comments.models in posts.models, This may happen due to circular import. Try this:

from posts import models as posts_models

and

post = models.ForeignKey(posts_models.Post,on_delete=models.CASCADE,related_name='comments')
danish_wani
  • 862
  • 9
  • 14
0

Try this, tell me if it helps

from DjangoPost.posts.models import Post

instead of

from posts.models import Post
Jai
  • 819
  • 7
  • 17