Get acces to manytomany field via model.values()

I have a model and i need to pass to my template only a certain manytomany field, but when i write something like this in get_queryset():


I get the following error:

get() returned more than one Book – it returned 5!

So, I’m wondering if there is a way to get rid of all fields except the specific manytomany and pass only those to the template

There’s more to this than what you’ve posted here. Please post the complete view involved.

Oh sure, sorry, but I don’t think there’s anything in the full code that broke it. I just get the name of the model and id that are specified in the url

class TitleDetail(DetailView):
    context_object_name = 'item'
    def get_queryset(self):
        if url=='book':
        elif url=='novel':
        return model

There’s your problem.

The DetailView CBV is designed around displaying a single object. You’re trying to display a list of objects. DetailView is the wrong base class for that. Since you’re going to be displaying multiple objects, you want to build this from a ListView.
(This may have been more obvious if you had posted the complete traceback and not just the last line.)

1 Like

Side note to this -


You really don’t want to do this. This is fragile and likely to break under any number of different circumstances.

Review the docs on urls to see how to configure your urls to accept those fields as parameters.

1 Like