Problem with view

19 views
Skip to first unread message

Maurizio Faccin

unread,
Feb 24, 2021, 6:55:05 PM2/24/21
to django...@googlegroups.com


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

Kasper Laudrup

unread,
Feb 24, 2021, 7:10:08 PM2/24/21
to django...@googlegroups.com
On 24/02/2021 21.00, Maurizio Faccin wrote:
>
> 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'
>

You have two classes called Fatture. In your models file and your views
file. The Python interpreter needs to figure out which one of them to use.

When you call the objects method on the Fatture class it is using the
class inheriting from models.Model but the tries to find the objects
attribute as a static member in the same class where you're making the call.

You could either make it explicit which class to use by prefixing the
call like *fattura.models.Fatture* but a better solution is probably to
rename the classes to FattureModel and FattureView. That would also
follow the naming patter from calling your serializer class
FatturaSerializer.

Kind regards,

Kasper Laudrup

Umberto Moffa

unread,
Feb 24, 2021, 7:20:32 PM2/24/21
to django...@googlegroups.com
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*

--
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...@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.

Maurizio Faccin

unread,
Feb 25, 2021, 1:46:41 AM2/25/21
to django...@googlegroups.com

I changed the classes name and works!
Thank you


Maurizio

Maurizio Faccin

unread,
Feb 25, 2021, 1:47:25 AM2/25/21
to django...@googlegroups.com

Ciao,
cambiando il nome delle classi funziona.

Grazie mille!


Maurizio

Umberto Moffa

unread,
Feb 25, 2021, 4:31:12 AM2/25/21
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages