Django REST Multiple Models (base_name & queryset) error

4

I am trying to use the Django REST Multiple Models and I have created the models and the viewset. But when trying to enter the url of the api, I get an error:

link

  

'base_name' argument not specified, and could not automatically   determine the name from the viewset, as it does not have a '.queryset'   attribute

Can anyone tell me what I'm missing? Since I have followed the documentation and I have the same structure as indicated, but being a little new with the Django API I do not know where this error comes from.

api / views.py

class PreguntasRespuestasViewset(MultipleModelAPIView):

    queryList = [
        (Pregunta.objects.all(), PreguntaSerializer),
        (Respuesta.objects.all(), RespuestaSerializer),
    ]

api / urls.py

router.register(r'faqs', PreguntasRespuestasViewset)

api / serializers.py

class PreguntaSerializer(ModelSerializer):
    class Meta:
        model = Pregunta
        fields = ('id', 'asunto', 'descripcion', 'fecha_publicacion')


class RespuestaSerializer(ModelSerializer):
    class Meta:
        model = Respuesta
        fields = ('Pregunta', 'contenido', 'mejor_respuesta')

models.py

class Pregunta(models.Model):
    asunto = models.CharField(max_length=200)
    descripcion = models.TextField()
    fecha_publicacion = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.asunto

    def publicado_hoy(self):
        return self.fecha_publicacion.date() == timezone.now().date()
    publicado_hoy.boolean = True
    publicado_hoy.short_description = 'Preguntado hoy'


class Respuesta(models.Model):
    Pregunta = models.ForeignKey(Pregunta)
    contenido = models.TextField()
    mejor_respuesta = models.BooleanField("Respuesta preferida", default=False)

    def __str__(self):
        return self.contenido

EDIT: If I type a base_name in the url:

router.register(r'faqs', PreguntasRespuestasViewset, base_name='PreguntasRespuestas')

and I get the following error:

  

as_view () takes exactly 1 argument (3 given)

    
asked by RuralGalaxy 24.05.2016 в 10:50
source

1 answer

3

The base_name parameter is not register , it is automatically generated when you have a queryset . In your case, as you are not declaring, what you should do is use as_view() .

urlpatterns = [
    url(r'^ faqs', PreguntasRespuestasViewset.as_view()),
]
    
answered by 19.10.2016 в 02:08