I Trying get list of objects using DRF, But Getting Error like "missing "Meta.model attribute"
Serialzers.py
from rest_framework import serializers
from .models import Car
class CarSerializer(serializers.ModelSerializer):
class Meta:
model: Car
fields=['brand_name','model_name','car_color']
Views.py Code Below:
from app2.serializers import CarSerializer
from rest_framework import generics
class BrandList(generics.ListCreateAPIView):
queryset = Brand.objects.all()
serializer_class = CarSerializer
URLS.py:
from app2.views import ,BrandList
path("BrandList/", BrandList.as_view(), name="BrandList"),
Please someone get out from this
Try models = instead of : in serializers.py
class CarSerializer(serializers.ModelSerializer):
class Meta:
model = Car
fields=['brand_name','model_name','car_color']
Related
I’m making a music player with a DRF backend.
I have two models, one is Song and the other is TrackQueue
In the browser, the “nowplaying” instance of TrackQueue shows the meta of the queued song with a link to the file in its meta.
What I need now is a url that always produces that instance of the “nowplaying” TrackQueue (id=1)
What would that url be and how can I create it?
Thank you
models.py
class Song(models.Model):
title = models.CharField(max_length=24)
file = models.FileField()
def __str__(self):
return self.title
class TrackQueue(models.Model):
title = models.CharField(max_length=64)
is_song = models.OneToOneField(Song, on_delete=models.CASCADE)
def __str__(self):
return self.title
Serializers.py
from rest_framework import serializers
from .models import Song, TrackQueue
class SongSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Song
fields = ('id' ,'title', 'file')
class TrackQueueSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TrackQueue
fields = ('id' , 'title', 'is_song')
views.py
from django.shortcuts import render
from .serializers import SongSerializer
from rest_framework import viewsets
from .models import Song, TrackQueue
from music.serializers import SongSerializer, TrackQueueSerializer
class SongView(viewsets.ModelViewSet):
serializer_class = SongSerializer
queryset = Song.objects.all()
class TrackQueueView(viewsets.ModelViewSet):
serializer_class = TrackQueueSerializer
queryset = TrackQueue.objects.all()
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from .models import Product
class ProductSerializer(serializers, ModelSerializer):
class Meta:
model = Product
fields = '__all__'
try this
from rest_framework import serializers
from .models import Product
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
I am trying to implement Rest api using Django framework. But when I click on the url on the default index page it gives me an assertion error at/languages/ Class LanguageSerializer missing meta.model attribute
I made all the migrations after changes in models.py but it did nothing
urls.py
from django.urls import path, include
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register('languages', views.LanguageView)
urlpatterns = [
path('', include(router.urls))
]
models.py
from django.db import models
class Language(models.Model):
name = models.CharField(max_length=50)
paradigm = models.CharField(max_length=50)
serializers.py
from rest_framework import serializers
from .models import Language
class LanguageSerializer(serializers.ModelSerializer):
class Meta:
fields = ('id', 'name', 'paradigm')
views.py
from django.shortcuts import render
from rest_framework import viewsets
from .models import Language
from .serializers import LanguageSerializer
class LanguageView(viewsets.ModelViewSet):
queryset = Language.objects.all()
serializer_class = LanguageSerializer
I have no clue where am I going wrong
You need to specify what model you want to serialize in the Meta class of your serializer, like:
from rest_framework import serializers
from .models import Language
class LanguageSerializer(serializers.ModelSerializer):
class Meta:
model = Language # specify the model
fields = ('id', 'name', 'paradigm')
otherwise the serializer can not determine the fields of that model, and how it will serialize the data from these fields.
When writing tests for Django Rest Framework, how are hyperlinked relations supposed to be created?
I'm trying to create a Foo object with a related Bar object, but I'm not sure how I can most efficiently create that relation.
# models.py
import uuid
from django.db import models
class Foo(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
bar = models.ForeignKey('Bar')
class Bar(models.Model):
BarUUID = models.UUIDField(primary_key=True, default=uuid.uuid4
# serializers.py
from rest_framework import serializers
from myapp.models import Bar, Foo
class FooSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Foo
fields = ('url', 'id', 'bar')
class BarSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Bar
fields = ('url', 'BarUUID')
# views.py
class FooViewSet(viewsets.ModelViewSet):
queryset = Foo.objects.all()
serializer_class = FooSerializer
class BarViewSet(viewsets.ModelViewSet):
queryset = Bar.objects.all()
serializer_class = BarSerializer
# urls.py
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'foos', views.FooViewSet)
router.register(r'bars', views.BarViewSet)
# tests.py
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
class FooTests(APITestCase):
def test_create_foo(self):
bar = Bar.objects.create()
url = reverse('foo-list')
data = {
'bar': # how do I get the URL?
}
response = self.client.post(url, data, format='json')
You can use the serializer to get it.
Like this:
from django.test.client import RequestFactory
from your_app.serializers import BarSerializer
context = {'request': RequestFactory().get('/')}
bar_serializer = BarSerializer(bar, context=context)
bar_serializer.data['url']
Straight forward way, without serializer:
url = 'http://testserver' + reverse('bar-detail', kwargs={'pk': bar.pk})
is there any way to show only a list of fields or excluding some of them when using django-rest-framework?
Here's my app/views.py:
from rest_framework.generics import ListAPIView
from .models import PhpbbUsers
class UsersReadView(ListAPIView):
model = PhpbbUsers
Obiously there are some user information that I don't want to show to everyone. How could I do?
Solution code
from rest_framework import generics, serializers
from .models import PhpbbUsers
class UsersSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = PhpbbUsers
fields = ('user_id', 'username', 'user_avatar')
class UsersReadView(generics.ListAPIView):
model = PhpbbUsers
serializer_class = UsersSerializer
Set the serializer_class attribute on the view.
See the quickstart for a good example: http://django-rest-framework.org/tutorial/quickstart.html