嗨,伙计们,我有问题从postgresql用django读取SQL数据. 我用www.example.com inspectdb创建了我的模型 命令来自现有数据库.
我的模型:
from django.db import models
class Regions(models.Model):
code = models.CharField(unique=True, max_length=4)
capital = models.CharField(max_length=10)
name = models.TextField(unique=True)
class Meta:
managed = False
db_table = "regions"
我的序列化器:
from rest_framework import serializers
class RegionsSerializer(serializers.ModelSerializer):
class Meta:
model = "france.models.Regions"
fields = "__all__"
帖子主题:Re:Колибри
from rest_framework import viewsets
from rest_framework.response import Response
from .models import Regions
from .serializers import RegionsSerializer
class RegionsView(viewsets.ViewSet):
def list(self, request):
regions = Regions.objects.all()
serializer = RegionsSerializer(regions, many=True)
return Response(serializer.data)
我得到的错误:
AttributeError at /regions/
'str' object has no attribute '_meta'
Request Method: GET
Request URL: http://127.0.0.1:1000/regions/
Django Version: 5.0.4
Exception Type: AttributeError
Exception Value:
'str' object has no attribute '_meta'
Exception Location: C:\Projects\play_ground\python\django_one\venv\Lib\site-packages\rest_framework\utils\model_meta.py, line 35, in get_field_info
Raised during: france.views.RegionsView
Python Executable: C:\Projects\play_ground\python\django_one\venv\Scripts\python.exe
Python Version: 3.12.2
Python Path:
['C:\\Projects\\play_ground\\python\\django_one',
'C:\\Users\\safkh\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip',
'C:\\Users\\safkh\\AppData\\Local\\Programs\\Python\\Python312\\DLLs',
'C:\\Users\\safkh\\AppData\\Local\\Programs\\Python\\Python312\\Lib',
'C:\\Users\\safkh\\AppData\\Local\\Programs\\Python\\Python312',
'C:\\Projects\\play_ground\\python\\django_one\\venv',
'C:\\Projects\\play_ground\\python\\django_one\\venv\\Lib\\site-packages']
Server time: Fri, 05 Apr 2024 08:19:46 +0000
你能帮我解决问题吗?