DRF在原始SQL查询中获得AttributeError

时间:2019-12-19 23:29:26

标签: python sql django django-models django-rest-framework

我正在尝试从我的Django代码制作一个原始的sql命令,尝试通过邮递员到达它时,我不断收到错误消息。我不确定问题出在哪里。

  

尝试获取字段值时出现AttributeError   profile_id在序列化程序AllDataSerializer

代码 Serializer.py

class AllDataSerializer(serializers.ModelSerializer):
class Meta:
    model = AllData
    fields = [
        'id',
        'profile_id',
        'first_name',
        'last_name',
        'tweet_text'
    ]

Views.py

class AllData(APIView):
permission_classes = [IsOwnerOrReadOnly]

def get(self, profile_id=None):

    with connection.cursor() as cursor:
        cursor.execute(
            "SELECT authapi_user.id AS profile_id, authapi_user.first_name, authapi_user.last_name, authapi_tweet.tweet_text FROM authapi_tweet INNER JOIN authapi_user ON authapi_user.id = authapi_tweet.author_id;")
        table = cursor.fetchone()
        data = AllDataSerializer(table).data
        return Response(data)

Models.py

class AllData(models.Model):
    profile_id = models.IntegerField()
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    tweet_text = models.CharField(max_length=200)

0 个答案:

没有答案