【问题标题】:Adding Model field from django views从 django 视图中添加模型字段
【发布时间】:2017-03-30 06:53:57
【问题描述】:

我正在使用 django-rest-framework 和 pandas 来制作一个 api,我的 models.py 文件看起来像这样

class Health(models.Model):
    Name = models.CharField(max_length=30,null=True,blank=True)
    Age = models.IntegerField(null=True,blank=True)
    Weight = models.FloatField(null=True,blank=True)
    Height = models.FloatField(null=True,blank=True)
    Sugar = models.FloatField(null=True,blank=True)
    def __str__(self):
        return self.Name

我的views.py文件看起来像这样

@api_view(['GET'])
def my_view(request,id):
    qs = Health.objects.filter(id = id)
    df = read_frame(qs)
    df['x-Mean'] = abs(df['Age'] - df['Age'].mean())
    df['1.96*std'] = 1.96*df['Age'].std()
    df['Outlier'] = abs(df['Age'] - df['Age'].mean()) > 1.96*df['Age'].std()
    df['BMI'] = df['Weight']/(df['Height']/100)**2
    a = df.fillna(0)
    a = a.to_dict(orient = 'records')
    return Response(a)

如您所见,我没有名为 BMI 的模型字段,因为我正在使用 pandas 数据框在我的视图中创建它,并且我想从 django 视图中保存该字段及其在我的 django 数据库中的相应数据。任何人都可以帮助我完成这项任务或提出适当的方法来做到这一点。

【问题讨论】:

  • 如果你知道你必须计算BMI并且总是保存它,为什么不预先添加字段而不是在视图中计算,你可以通过覆盖它在save方法中计算在模型中。
  • @AKS 你能帮我改正我的代码吗

标签: python django pandas django-rest-framework


【解决方案1】:

正如 AKS 所说,您需要在 django 模型中为 BMI 创建字段,然后使用保存在视图中更新该字段。 以下是你的做法:

class Health(models.Model):
    Name = models.CharField(max_length=30,null=True,blank=True)
    Age = models.IntegerField(null=True,blank=True)
    Weight = models.FloatField(null=True,blank=True)
    Height = models.FloatField(null=True,blank=True)
    Sugar = models.FloatField(null=True,blank=True)
    BMI =  models.FloatField(null=True,blank=True)
    def __str__(self):
        return self.Name

在你看来,这样做:

@api_view(['GET'])
def my_view(request,id):
    qs = Health.objects.filter(id = id)
    df = read_frame(qs)
    df['x-Mean'] = abs(df['Age'] - df['Age'].mean())
    df['1.96*std'] = 1.96*df['Age'].std()
    df['Outlier'] = abs(df['Age'] - df['Age'].mean()) > 1.96*df['Age'].std()
    df['BMI'] = df['Weight']/(df['Height']/100)**2

    # once the BMI is caluculated update the DB row with the value
    qs.BMI = df['BMI']
    qs.save()


    a = df.fillna(0)
    a = a.to_dict(orient = 'records')

    return Response(a)

试试这个:

查看功能:

@api_view(['GET'])
def my_view(request,id):
    qs = Health.objects.filter(id = id)
    l = len(qs)

    if l>0:
        for x in qs:
            df = read_frame(x)
            df['x-Mean'] = abs(df['Age'] - df['Age'].mean())
            df['1.96*std'] = 1.96*df['Age'].std()
            df['Outlier'] = abs(df['Age'] - df['Age'].mean()) > 1.96*df['Age'].std()
            df['BMI'] = df['Weight']/(df['Height']/100)**2


            # once the BMI is caluculated update the DB row with the value
            x.BMI = df['BMI']
            x.save()

            a = df.fillna(0)
            a = a.to_dict(orient = 'records')


    else:
        print "no rows exists for the query "

希望这是有用的。

【讨论】:

  • 我收到以下错误 'QuerySet' object has no attribute 'save'
  • 我使用了 qs.update() 但我的数据库中仍然没有保存任何内容
  • 尝试使用这个查询:qs = Health.objects.get(id = id)
  • qs = Health.objects.filter(id = id) ,过滤器用于从db中获取多行。如果您想获得多行,请使用它并遍历对象并修改每个对象。但是如果你只想查询一行,你应该使用 qs = Health.objects.get(id = id)
  • 收到错误消息“健康”对象没有属性“_iterable_class”
【解决方案2】:
@api_view(['GET'])
def my_view(request,id):
    qs = Health.objects.filter(id = id)
    df = read_frame(qs)
    df['x-Mean'] = abs(df['age'] - df['age'].mean())
    df['1.96*std'] = 1.96*df['age'].std()
    df['outlier'] = abs(df['age'] - df['age'].mean()) > 1.96*df['age'].std()
    df['bmi'] = df['weight']/(df['height']/100)**2
    for q in qs:
        q.bmi = df['bmi'][0]
        q.save()
    a = df.fillna(0)
    a = a.to_dict(orient = 'records')
    return Response(a)

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2012-06-05
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多