访问通过User模型链接到不同模型的Django模型上的字段

时间:2018-03-04 02:29:50

标签: django django-models

我有以下型号:

class Reputation(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    score = models.IntegerField(default=0)
    ....

class Article(models.Model):
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL)
    ....

我正在尝试从Article对象访问信誉模型上的“score”字段。我在Article对象(article_object)上尝试了以下操作,但它不起作用:

article_object.created_by.reputation_set.score  

有什么办法可以从文章对象中获取“得分”吗?谢谢。

1 个答案:

答案 0 :(得分:0)

改为使用OneToOneField

related_name自动创建一个后向引用,其模型名称为小写。

您可以通过在字段上设置function [hundreds, tens, units] = seperateThreeDigits(x) if (x<1 ||x>999) fprintf('Error! Please enter an integer between 1 and 999 and try again'); hundreds = []; tens = []; units = []; else units = mod(x, 10); tens = mod(floor(x/10), 10); hundreds = mod(floor(x/100), 10); fprintf('h=%d, t=%d, u=%d.\n', hundreds, tens, units); end end function prob1() factorion = []; factorions = []; for x = 1:999 [hundreds, tens, units] = separateThreeDigits(x); if x < 10 factorion = factorial(units); if factorion ==x factorions = [factorions, x]; end elseif x < 100 factorion = factorial(tens) + factorial(units); if factorion ==x factorions = [factorions, x]; end else factorion = factorial(hundreds) + factorial(tens) + factorial(units); if factorion ==x factorions = [factorions, x]; end end end end 来覆盖相关的对象名称。

来源