Hello,
I'am a newbie in usign django and django rest.
I have a problem with a modelsetview
The message error is:
Exception Type: | AttributeError |
---|---|
Exception Value: | type object 'Fatture' has no attribute 'objects' |
Exception Location: | C:\Lavoro\Python\scambiosdi\scambiosdi\fatture\api\views.py, line 15, in get_queryset |
This is the model.py:
from django.db import models
from scambiosdi import dataset
from utenti.models import Utenti
class Fatture(models.Model):
utente = models.ForeignKey(Utenti, on_delete=models.CASCADE, related_name="fatture")
tipo_fattura = models.IntegerField(choices=dataset.TIPOFATTURA)
denominazione = models.CharField(max_length=80)
numero = models.CharField(max_length=20)
data = models.DateField()
totale = models.DecimalField(max_digits=18, decimal_places=2, default=0)
identificativo_sdi = models.CharField(max_length=50, blank=True)
percorso_file_originale = models.CharField(max_length=240, blank=True)
percorso_file_pulito = models.CharField(max_length=240, blank=True)
class Meta():
verbose_name = 'Fattura'
verbose_name_plural = 'Fatture'
def __str__(self):
return '{} {} {}'.format(self.denominazione, self.numero, self.data)
This is the serializers.py:
from rest_framework import serializers
from fatture.models import Fatture
class FatturaSerializer(serializers.ModelSerializer):
user = serializers.StringRelatedField(read_only=True)
class Meta:
model = Fatture
fields = "__all__"
And this is the views.py:
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated
from fatture.models import Fatture
from fatture.api.serializers import FatturaSerializer
class Fatture(ModelViewSet):
serializer_class = FatturaSerializer
permission_classes = [IsAuthenticated]
def get_queryset(self):
queryset = Fatture.objects.all()
idfattura = self.request.query_params.get("idfattura", None)
if idfattura is not None:
queryset = queryset.filter(id=idfattura)
return queryset
If I change the Fatture class in this way works correctly:
class Fatture(ModelViewSet):
queryset = Fatture.objects.all()
serializer_class = FatturaSerializer
permission_classes = [IsAuthenticated]
Maurizio
No comments:
Post a Comment