Python DRF在原始sql查询中获得AttributeError
我试图从我的django代码中生成一个原始sql命令,当我试图通过Postman访问它时,我不断地得到一个错误。我不确定问题出在哪里 尝试获取字段的值时获取AttributeError 序列化程序上的配置文件id所有数据序列化程序Python DRF在原始sql查询中获得AttributeError,python,sql,django,django-models,django-rest-framework,Python,Sql,Django,Django Models,Django Rest Framework,我试图从我的django代码中生成一个原始sql命令,当我试图通过Postman访问它时,我不断地得到一个错误。我不确定问题出在哪里 尝试获取字段的值时获取AttributeError 序列化程序上的配置文件id所有数据序列化程序 代码 序列化程序.py class AllDataSerializer(serializers.ModelSerializer): class Meta: model = AllData fields = [ 'id',
代码
序列化程序.py
class AllDataSerializer(serializers.ModelSerializer):
class Meta:
model = AllData
fields = [
'id',
'profile_id',
'first_name',
'last_name',
'tweet_text'
]
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)
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)
视图.py
class AllDataSerializer(serializers.ModelSerializer):
class Meta:
model = AllData
fields = [
'id',
'profile_id',
'first_name',
'last_name',
'tweet_text'
]
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)
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)
型号.py
class AllDataSerializer(serializers.ModelSerializer):
class Meta:
model = AllData
fields = [
'id',
'profile_id',
'first_name',
'last_name',
'tweet_text'
]
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)
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)
能否通过python manage.py shell
测试游标执行和AllDataSerializer
使用情况?如果一切正常,那么我认为isownerreadonly
permission类存在问题。我得到了相同的错误变量是否包含profile\u id
属性?