django - Models not linked after resolving circular import issue -


i'm creating app allows users create reviews of locations. had issue circular imports in django resolved after looking @ post: django - circular model import issue

what did rid of import in locations.models , lazy import of review model. in django admin, can create review , assign location. however, when open location in django admin, doesn't have linked reviews.

haven't found far suggest why location not showing review created. or tips appreciated.

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) 

if have foreign key review.location, means each review belongs 1 location.

class review(models.model):     ...     location = models.foreignkey(location, on_delete=models.cascade) 

you can follow relationship backwards location see reviews.

location.review_set.all() 

see docs on following relationships backwards more info.

you don't need define foreign key on both models. defining foreign key location.review means each location has 1 review (or no reviews), doesn't make sense. setting review.location not update reviews field location, separate fields.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -