Django AppRegistryNotReady:models arent loaded yet- reverse Foreign Key query between two appsRadio buttons in django adminFor statement in django templates doesn't workDjango Issue: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yetDjango Apps aren't loaded yetHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldDjango Twilio Texting - django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yetHow to define Mode with generic ForeignKey in DjangoHow to remove Django redundant inner joinecommerce customer custom login auth in django python

Who is "He that flies" in Lord of the Rings?

The origin of the Russian proverb about two hares

How can I remove material from this wood beam?

How to befriend someone who doesn't like to talk?

What is the Leave No Trace way to dispose of coffee grounds?

Could a person damage a jet airliner - from the outside - with their bare hands?

Diatonic chords of a pentatonic vs blues scale?

Do you need to let the DM know when you are multiclassing?

Are polynomials with the same roots identical?

So a part of my house disappeared... But not because of a chunk resetting

What should I be wary of when insurer is taking a lot of time to decide whether car is repairable or a total loss?

What would be the way to say "just saying" in German? (Not the literal translation)

How to write a convincing religious myth?

Was planting UN flag on Moon ever discussed?

Does putting salt first make it easier for attacker to bruteforce the hash?

How far would a landing Airbus A380 go until it stops with no brakes?

Does the Nuka-Cola bottler actually generate nuka cola?

What differences exist between adamantine and adamantite in all editions of D&D?

Why do radiation hardened IC packages often have long leads?

Difference between prepositions in "...killed during/in the war"

Is it a acceptable way to write a loss function in this form?

What is the logic behind charging tax _in the form of money_ for owning property when the property does not produce money?

Why isn't Bash trap working if output is redirected to stdout?

Confused with atmospheric pressure equals plastic balloon’s inner pressure



Django AppRegistryNotReady:models arent loaded yet- reverse Foreign Key query between two apps


Radio buttons in django adminFor statement in django templates doesn't workDjango Issue: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yetDjango Apps aren't loaded yetHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldDjango Twilio Texting - django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yetHow to define Mode with generic ForeignKey in DjangoHow to remove Django redundant inner joinecommerce customer custom login auth in django python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to create a ratings app for a news aggregation site, but I think either my implementation of the apps or the foreignkey queries from ratings to articles is wrong. I keep getting



raise AppRegistryNotReady("Models aren't loaded yet.")


django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.



when migrating. The idea is that each user's individual rating of an article is handled by ArticleRating, and then each article would have an average score handled by OverallArticleRating



I think the issue goes down to one of 3 things:
1. the user ratings should be implemented as a model in the articles app, then referenced by the OverallRatings model.
2. my foreign key queryset syntax is incorrect. I have tried using article.articlerating_set for the queryset of ArticleRating objects, but that only returns attribute not defined.
3. I need to reference the article object associated with each rating object differently.



from django.db import models
from users.models import User
from Articles.models import Article





class AbstractRating(models.Model):
score = models.IntegerField()



def __str__(self):
return str(self.score)

class Meta:
abstract = True
ordering= ['-score']

class ArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
rater = models.ForeignKey(User, on_delete=models.CASCADE)

class OverallArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
def getArticleAverageScore(art):
sumUserScore = 0
averageUserScore = 0
ratingSet = ArticleRating.objects.filter(article=art)
if len(ratingSet) > 0:
for userRating in userRatingSet:
sumUserScore += userRating.score
averageUserScore = sumUserScore/(ArticleRating.objects.filter( article=art).count())
return averageUserScore
else:
return 0

score = getArticleAverageScore(article)









share|improve this question
























  • You should try to use '<app_name>.<model_name>' (string) instead of direct import in your models. article = models.ForeignKey("article.Article", on_delete=models.CASCADE)

    – MrBinWin
    Mar 25 at 3:54

















0















I am trying to create a ratings app for a news aggregation site, but I think either my implementation of the apps or the foreignkey queries from ratings to articles is wrong. I keep getting



raise AppRegistryNotReady("Models aren't loaded yet.")


django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.



when migrating. The idea is that each user's individual rating of an article is handled by ArticleRating, and then each article would have an average score handled by OverallArticleRating



I think the issue goes down to one of 3 things:
1. the user ratings should be implemented as a model in the articles app, then referenced by the OverallRatings model.
2. my foreign key queryset syntax is incorrect. I have tried using article.articlerating_set for the queryset of ArticleRating objects, but that only returns attribute not defined.
3. I need to reference the article object associated with each rating object differently.



from django.db import models
from users.models import User
from Articles.models import Article





class AbstractRating(models.Model):
score = models.IntegerField()



def __str__(self):
return str(self.score)

class Meta:
abstract = True
ordering= ['-score']

class ArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
rater = models.ForeignKey(User, on_delete=models.CASCADE)

class OverallArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
def getArticleAverageScore(art):
sumUserScore = 0
averageUserScore = 0
ratingSet = ArticleRating.objects.filter(article=art)
if len(ratingSet) > 0:
for userRating in userRatingSet:
sumUserScore += userRating.score
averageUserScore = sumUserScore/(ArticleRating.objects.filter( article=art).count())
return averageUserScore
else:
return 0

score = getArticleAverageScore(article)









share|improve this question
























  • You should try to use '<app_name>.<model_name>' (string) instead of direct import in your models. article = models.ForeignKey("article.Article", on_delete=models.CASCADE)

    – MrBinWin
    Mar 25 at 3:54













0












0








0








I am trying to create a ratings app for a news aggregation site, but I think either my implementation of the apps or the foreignkey queries from ratings to articles is wrong. I keep getting



raise AppRegistryNotReady("Models aren't loaded yet.")


django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.



when migrating. The idea is that each user's individual rating of an article is handled by ArticleRating, and then each article would have an average score handled by OverallArticleRating



I think the issue goes down to one of 3 things:
1. the user ratings should be implemented as a model in the articles app, then referenced by the OverallRatings model.
2. my foreign key queryset syntax is incorrect. I have tried using article.articlerating_set for the queryset of ArticleRating objects, but that only returns attribute not defined.
3. I need to reference the article object associated with each rating object differently.



from django.db import models
from users.models import User
from Articles.models import Article





class AbstractRating(models.Model):
score = models.IntegerField()



def __str__(self):
return str(self.score)

class Meta:
abstract = True
ordering= ['-score']

class ArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
rater = models.ForeignKey(User, on_delete=models.CASCADE)

class OverallArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
def getArticleAverageScore(art):
sumUserScore = 0
averageUserScore = 0
ratingSet = ArticleRating.objects.filter(article=art)
if len(ratingSet) > 0:
for userRating in userRatingSet:
sumUserScore += userRating.score
averageUserScore = sumUserScore/(ArticleRating.objects.filter( article=art).count())
return averageUserScore
else:
return 0

score = getArticleAverageScore(article)









share|improve this question
















I am trying to create a ratings app for a news aggregation site, but I think either my implementation of the apps or the foreignkey queries from ratings to articles is wrong. I keep getting



raise AppRegistryNotReady("Models aren't loaded yet.")


django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.



when migrating. The idea is that each user's individual rating of an article is handled by ArticleRating, and then each article would have an average score handled by OverallArticleRating



I think the issue goes down to one of 3 things:
1. the user ratings should be implemented as a model in the articles app, then referenced by the OverallRatings model.
2. my foreign key queryset syntax is incorrect. I have tried using article.articlerating_set for the queryset of ArticleRating objects, but that only returns attribute not defined.
3. I need to reference the article object associated with each rating object differently.



from django.db import models
from users.models import User
from Articles.models import Article





class AbstractRating(models.Model):
score = models.IntegerField()



def __str__(self):
return str(self.score)

class Meta:
abstract = True
ordering= ['-score']

class ArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
rater = models.ForeignKey(User, on_delete=models.CASCADE)

class OverallArticleRating(AbstractRating):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
def getArticleAverageScore(art):
sumUserScore = 0
averageUserScore = 0
ratingSet = ArticleRating.objects.filter(article=art)
if len(ratingSet) > 0:
for userRating in userRatingSet:
sumUserScore += userRating.score
averageUserScore = sumUserScore/(ArticleRating.objects.filter( article=art).count())
return averageUserScore
else:
return 0

score = getArticleAverageScore(article)






django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 0:48







lz2313

















asked Mar 24 at 21:42









lz2313lz2313

403




403












  • You should try to use '<app_name>.<model_name>' (string) instead of direct import in your models. article = models.ForeignKey("article.Article", on_delete=models.CASCADE)

    – MrBinWin
    Mar 25 at 3:54

















  • You should try to use '<app_name>.<model_name>' (string) instead of direct import in your models. article = models.ForeignKey("article.Article", on_delete=models.CASCADE)

    – MrBinWin
    Mar 25 at 3:54
















You should try to use '<app_name>.<model_name>' (string) instead of direct import in your models. article = models.ForeignKey("article.Article", on_delete=models.CASCADE)

– MrBinWin
Mar 25 at 3:54





You should try to use '<app_name>.<model_name>' (string) instead of direct import in your models. article = models.ForeignKey("article.Article", on_delete=models.CASCADE)

– MrBinWin
Mar 25 at 3:54












1 Answer
1






active

oldest

votes


















0














The problem is the last line, where you're calling getArticleAverageScore at class level That's doesn't make any sense; you need to call it with an instance of the model, but as it is it's being called at definition time before there are any instances at all.



If you need a score attribute that always reflects the average score, then use a property:



@property
def score(self):
sumUserScore = 0
sumUserScore = 0
averageUserScore = 0
ratingSet = ArticleRating.objects.filter(article=set)
...


However, also note that your method is really inefficient. You should use aggregation instead.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328855%2fdjango-appregistrynotreadymodels-arent-loaded-yet-reverse-foreign-key-query-be%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The problem is the last line, where you're calling getArticleAverageScore at class level That's doesn't make any sense; you need to call it with an instance of the model, but as it is it's being called at definition time before there are any instances at all.



    If you need a score attribute that always reflects the average score, then use a property:



    @property
    def score(self):
    sumUserScore = 0
    sumUserScore = 0
    averageUserScore = 0
    ratingSet = ArticleRating.objects.filter(article=set)
    ...


    However, also note that your method is really inefficient. You should use aggregation instead.






    share|improve this answer



























      0














      The problem is the last line, where you're calling getArticleAverageScore at class level That's doesn't make any sense; you need to call it with an instance of the model, but as it is it's being called at definition time before there are any instances at all.



      If you need a score attribute that always reflects the average score, then use a property:



      @property
      def score(self):
      sumUserScore = 0
      sumUserScore = 0
      averageUserScore = 0
      ratingSet = ArticleRating.objects.filter(article=set)
      ...


      However, also note that your method is really inefficient. You should use aggregation instead.






      share|improve this answer

























        0












        0








        0







        The problem is the last line, where you're calling getArticleAverageScore at class level That's doesn't make any sense; you need to call it with an instance of the model, but as it is it's being called at definition time before there are any instances at all.



        If you need a score attribute that always reflects the average score, then use a property:



        @property
        def score(self):
        sumUserScore = 0
        sumUserScore = 0
        averageUserScore = 0
        ratingSet = ArticleRating.objects.filter(article=set)
        ...


        However, also note that your method is really inefficient. You should use aggregation instead.






        share|improve this answer













        The problem is the last line, where you're calling getArticleAverageScore at class level That's doesn't make any sense; you need to call it with an instance of the model, but as it is it's being called at definition time before there are any instances at all.



        If you need a score attribute that always reflects the average score, then use a property:



        @property
        def score(self):
        sumUserScore = 0
        sumUserScore = 0
        averageUserScore = 0
        ratingSet = ArticleRating.objects.filter(article=set)
        ...


        However, also note that your method is really inefficient. You should use aggregation instead.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 7:34









        Daniel RosemanDaniel Roseman

        469k42606664




        469k42606664





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328855%2fdjango-appregistrynotreadymodels-arent-loaded-yet-reverse-foreign-key-query-be%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript