python - need to access a manytomanyfield in django templates -


so have these models:

class cofifiuser(models.model):   user = models.onetoonefield(user)  class quoteidea(models.model):   creator = models.foreignkey(cofifiuser,related_name="creator")   text = models.textfield(max_length=250)   votes = models.charfield(max_length=100,default="0")   votes_received = models.manytomanyfield(cofifiuser)   created_at = models.datetimefield(auto_now_add=true) 

and want:

if request.user.username in item.votes_received.all    <button class="disabled">button</button> else    <button class="btn btn-primary">button</button> 

is same thing button on facebook . cannot give more 1 page ( in python/django ) please need here :)

something that's important keep in mind here performance implication of having query many-to-many relationship each comparison, or pre-loading of m2m data see if user in votes_received queryset.

in case this, opt de-normalized way boolean comparison. create field hold ids comma-separated ints , update field via post_save signal.

this simplifies things @ view , template level , avoids having joins, or additional queries @ all.

assuming you're looping on list of quoteidea instances , passing instance of cofifiuser template, , have added field quoteidea called cofifi_vote_ids can do:

{# returns added prevent wrapping #}  {% quote_idea in quote_ideas %}     <button class="btn          {% if cofifi_user.id in quote_idea.cofifi_vote_ids %}         disabled         {% else %}         btn-primary         {% endif %}">button</button> {% endfor %} 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -