解决循环导入问题后未链接的模型

时间:2021-12-04 18:07:22

I'm creating an app which allows for users to create reviews of locations. I had an issue with circular imports in Django which I resolved after looking at this post: Django - Circular model import issue

我正在创建一个应用程序,允许用户创建位置评论。我在Django中遇到了循环导入的问题,我在查看这篇文章后解决了这个问题:Django - 循环模型导入问题

What I did was get rid of the import in locations.models and do a lazy import of my Review model. In the Django admin, I can create a Review and assign it to a Location. However, when I open that Location in Django admin, it doesn't have any linked reviews.

我所做的是摆脱locations.models中的导入并对我的Review模型进行惰性导入。在Django管理员中,我可以创建一个Review并将其分配给一个Location。但是,当我在Django admin中打开该位置时,它没有任何链接的评论。

Haven't found anything so far to suggest why my location is not showing the review that I created. Any help or tips are much appreciated.

到目前为止还没有找到任何建议为什么我的位置没有显示我创建的评论。任何帮助或提示非常感谢。

Django = 1.9
Python = 2.7

Django = 1.9 Python = 2.7

Thanks



locations.models.py:

class Location(models.Model):
    ...
    reviews = models.ForeignKey('reviews.Review', null=True, blank=True, related_name="location_reviews")

reviews.models.py:

from locations.models import Location

class Review(models.Model):
    ...
    location = models.ForeignKey(Location, on_delete=models.CASCADE)

1 个解决方案

#1


0  

If you have a foreign key Review.location, that means that each review belongs to one location.

如果您有外键Review.location,则表示每个评论都属于一个位置。

class Review(models.Model):
    ...
    location = models.ForeignKey(Location, on_delete=models.CASCADE)

You can then follow the relationship backwards from a location to see its reviews.

然后,您可以从某个位置向后关注该关系以查看其评论。

location.review_set.all()

See the docs on following relationships backwards for more info.

有关详细信息,请参阅后面关系的文档。

You probably don't need to define a foreign key on both models. Defining a foreign key Location.review means that each location has one review (or no reviews), which doesn't really make sense. Setting review.location does not update the reviews field for that location, they are completely separate fields.

您可能不需要在两个模型上定义外键。定义外键Location.review意味着每个位置都有一个评论(或没有评论),这没有多大意义。设置review.location不会更新该位置的评论字段,它们是完全独立的字段。

#1


0  

If you have a foreign key Review.location, that means that each review belongs to one location.

如果您有外键Review.location,则表示每个评论都属于一个位置。

class Review(models.Model):
    ...
    location = models.ForeignKey(Location, on_delete=models.CASCADE)

You can then follow the relationship backwards from a location to see its reviews.

然后,您可以从某个位置向后关注该关系以查看其评论。

location.review_set.all()

See the docs on following relationships backwards for more info.

有关详细信息,请参阅后面关系的文档。

You probably don't need to define a foreign key on both models. Defining a foreign key Location.review means that each location has one review (or no reviews), which doesn't really make sense. Setting review.location does not update the reviews field for that location, they are completely separate fields.

您可能不需要在两个模型上定义外键。定义外键Location.review意味着每个位置都有一个评论(或没有评论),这没有多大意义。设置review.location不会更新该位置的评论字段,它们是完全独立的字段。