Home:ALL Converter>How do I get django template to recognize manytomanyfield comparison?

How do I get django template to recognize manytomanyfield comparison?

Ask Time:2018-08-17T05:37:24         Author:Steve Smith

Json Formatter

In my project, I am currently trying to access a manytomany field and compare it to another. I have several cases where I am doing something similar and it is working . The difference in this case is that I am trying to essentially say if one of the values in this many to many field equals a value in this other manytomanyfield, then do something....

Here is my code...

Book(models.Model):
    publisher = models.ManyToManyField(Name)

Userprofile(models.Model):
    publisher = models.ManyToManyField(Name)

In my Django template I am trying to do something like...

{% If user_publisher in form.initial.publisher_set.all %} 
{{ publisher.name }}
{% endif %}

The example above is a simplified version of what I'm trying to do....I'm essentially trying to compare the manytomany fields and if any of the values match, perform an action. I've been at this most of today and have tried several combinations.

If I do something like

{% if user in form.initial.publisher.all %}

This works fine. I'm struggling to try and figure out how I can compare manytomanyfields. I suspect the user query works fine because it's not a manytomanyfield.

I'm thinking my format is off. I have tried to use the _set to publisher and the user publisher and when I go so far as to print the output, I am actually seeing that both the user_publisher and publisher querysets are the same. However, my django template is not showing me any results. Thanks in advance for any thoughts.

I have surfed SO all afternoon as well as Google, but can't quite figure out what I'm doing wrong.

Here is more detail to my issue. I am currently doing a CreateView whereby I am trying to get an existing record by overriding get_initial as shown below:

class BookUpdateView(CreateView):
    model = Book 
    form_class = Book
    template_name = 'Book/update_book.html'

    def get_initial(self):
        initial = super(BookUpdateView, self).get_initial()
        book = Book.objects.get(pk=self.kwargs["pk"])
        initial = book.__dict__.copy()

        initial.update({
             "publisher": publisher.all(),
        })
        return initial

Because I am copying in these records as a starting point and this is a CreateView, I don't yet have a PK or ID to query from a get_context_data perspective. Not sure how to go about getting the data that I am copying in but have not yet saved. I am actually trying to figure out if a user has the same publisher via their user profile and was able to figure out the format for get_context_data as shown below:

def get_context_data(self, **kwargs):
    context = super(BookUpdateView, self).get_context_data(**kwargs)
    user_publisher = self.request.user.userprofile.publisher.all()
    return context  

I tried to do something like....

{% if user_publisher in form.initial.publisher_set.all %} 

But the template never recognizes that these two in fact do match...

When I print the variables....

They both show...

<QuerySet [<Publisher: ACME Publishing>]>
<QuerySet [<Publisher: ACME Publishing>]>

But the template does not recognize that they are the same. The screen is not rendered as I would expect when using the template language above. No errors, but end result isn't what I would expect either. Thanks in advance for any additional thoughts.

Author:Steve Smith,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51885656/how-do-i-get-django-template-to-recognize-manytomanyfield-comparison
yy