Wednesday, February 24, 2021

Re: Problem with view


Ciao,
cambiando il nome delle classi funziona.

Grazie mille!


Maurizio

Il 25/02/2021 01:14, Umberto Moffa ha scritto:
Ciao, prova a cambiare il nome delle classi, forse chiamandosi Fatture sia il Model che la View va in pappa, oppure sovrascrivi il nome del Model quando lo importi nella view

Esempio:
"from model. import Fatture as *qualsiasi altro nome*

Il giorno gio 25 feb 2021 alle 00:54 Maurizio Faccin <maurizio.faccin@tiscali.it> ha scritto:


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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d1883242-9b48-a158-1e27-cd021c60fd85%40tiscali.it.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFdc%3DxTYDDDb0yKme0Nsu-73ORhfpf9938%2B8nS3wFkZf6SxRcQ%40mail.gmail.com.


No comments:

Post a Comment