Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

issue in Django 5.0.2 and django_rest_framework

hi guys i have issue with reading sql data from postgresql with django.
i created my model with manage.py inspectdb
command from existing database.

my model:

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"

My serializer:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from rest_framework import serializers


class RegionsSerializer(serializers.ModelSerializer):
    class Meta:
        model = "france.models.Regions"
        fields = "__all__"


my viewset:

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)

and error i get:

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

can you help me with issue thnx all

>Solution :

The serializer Meta can not work with a string as model, but should refer to the model class, so:

from france.models import Regions
from rest_framework import serializers


class RegionsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Regions
        fields = '__all__'

Note: normally a Django model is given a singular name, so Region instead of Regions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading