从django视图添加模型字段

时间:2017-03-30 06:53:58

标签: python django pandas django-rest-framework

我正在使用带有pandas的django-rest-framework来创建一个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数据库中的相应数据。任何人都可以帮助我完成这项任务或建议一个合适的方法来做到这一点。

2 个答案:

答案 0 :(得分:0)

正如AKS所说,您需要在django模型中为BMI创建字段,然后使用save更新视图中的该字段。 这是你如何做到的:

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 "

希望这很有用。

答案 1 :(得分:0)

@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)