155👍
Try
matches = [val for val in Store.attribute_answers.all() if val in WishList.attribute_answers.all()]
Notice the parenthesis at the end of WishList.attribute_answers.all()
. Adding the parenthesis invokes the all
function to return an iterable.
If you include the parenthesis you’re saying “give me all the values in the stores answers so long as that value is also in the wish lists answers”. Without the parenthesis you’re asking for all the values from the store’s answers that are also in the all
function, which is meaningless. The all function is not an iterable (it’s a function that returns an iterable)
- [Django]-Complete django DB reset
- [Django]-Django error when installing Graphite – settings.DATABASES is improperly configured. Please supply the ENGINE value
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
61👍
If you are doing this in a template:
{% for room in study.room_choice.all %}
{{ room }}
{% empty %}
empty list!
{% endfor %}
UPDATE
If you have a through table, you can access the elements in that table (as detailed here) like so (note, you use the through table name, in lowercase, suffixing _set):
{% for roominfo in participant.roomchoicethru_set.all %}
{{ roominfo.room}} {{ roominfo.telnumber}}
{% endfor %}
- [Django]-AngularJS with Django – Conflicting template tags
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
- [Django]-How to rename items in values() in Django?
48👍
all()
For everyone who finds reading code in questions as TL;DR
Instead of query_set.many_to_many
you should use
query_set.many_to_many.all()
- [Django]-How to properly use the "choices" field option in Django
- [Django]-Where to put business logic in django
- [Django]-Django-tables2: How to use accessor to bring in foreign columns?
0👍
Answers are good and I just needed to add a point for easier understanding.You couldn’t query the field where many to many is set because it returns and objects not a list of options.It is better to get the data from the field in your views then pass it to the templates.For example if you have posts model where each post has many to many relations with Tags model.
class Tag(models.Model):
""
class Post(models.Model):
tags = models.ManyToManyField(Tag)
If you tried post.tag.caption it will return this error.You should pass tags as a seperate idintifier in your views.
"post": identified_post,
"post_tags":identified_post.tags.all()
instead of passing only
"post": identified_post
- [Django]-Django DB Settings 'Improperly Configured' Error
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-How can I save my secret keys and password securely in my version control system?
-1👍
I keep hitting this question whenever this problem comes up. Particularly when trying to actually iterate over a manytomany in a function.
As a template you can do:
array = many_to_many.all()
for x in many_to_many:
function here
- [Django]-Django models.py Circular Foreign Key
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-How to resize the new uploaded images using PIL before saving?
-2👍
Here busines_type is foreign_key in profile model
pro = Profile.object.filter(user=myuser).first()
business_type = pro.business_type.all()
if business_type:
b_type = ''
for b in business_type:
b_type += str(b.type)+' '
a = b_type
- [Django]-How to stop autopep8 not installed messages in Code
- [Django]-Good ways to sort a queryset? – Django
- [Django]-H14 error in heroku – "no web processes running"