44👍
There is no need to turn the entries QuerySet into a list. Additionally, you can let the DB do the sorting using order_by.
entries = Entry.objects.filter(user_id=user_id).order_by('id')
Add .all
to get all the values from a relationship (just like Entry.objects.all()
).
entry.tags.all
You can try this in the shell as well (I use ipython so your output may look different):
$ ./manage.py shell
# ...
In [1]: from yourproject.models import Entry, Tags
In [2]: entry = Entry.objects.all()[0]
In [3]: entry.tags
Out[3]: <django.db.models.fields.related.ManyRelatedManager object at 0x...>
In [4]: entry.tags.all() # for an entry with no tags.
Out[4]: []
In [5]: # add a few tags
In [6]: for n in ('bodywork', 'happy', 'muscles'):
...: t, created = Tag.objects.get_or_create(name=n)
...: entry.tags.add(t)
In [7]: entry.tags.all()
Out[7]: [<Tag: ...>, <Tag: ...>, <Tag: ...>]
And if you want to call out the entries with zero tags use for..empty.
{% for tag in entry.tags.all %}
{{ tag.name }}
{% empty %}
No tags!
{% endfor %}
11👍
Here is the solution of your query,
Verifying your solution by giving an example
Suppose a book have number of tags, so in order to display all the tags of a book on template can be like this
{% for tag in book.tags.all %}
{{ tag.name }}
{% endfor %}
where the model of Tag is like,
class Tag(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return "%s" % unicode(self.name)
- Django's {{ csrf_token }} is outputting the token value only, without the hidden input markup
- Django-Rest-Framework serializer class meta
5👍
OK. I found the problem. I had some incorrect code which was commented out. But Django processed that code. So html comments didn’t work here. I fixed this and it all worked like a charm.
So if you didn’t know – the html comments don’t prevent template processing.
This is because the template is being processed by Django first then HTML is rendered by browser.
- Unknown column '' in 'field list'. Django
- Creating UTF-8 JsonResponse in Django
- How do you access query params in a Django Rest Framework 3.0 serializer?
- Django Check and set permissions for a user group
- Django admin dropdown of 1000s of users
4👍
The above from istruble is correct but if your question contains all of your code you need to specify a property in your template:
{% for entry in entries %}
{{ entry.title }}
{{ entry.date }}
{% for tag in entry.tags.all %} {{ tag.name }} {% endfor %}
{% endfor %}
or a default unicode function to your model:
class Tag(models.Model):
name = models.CharField(max_length=20)
def __unicode__(self):
return self.name
- Sphinx and re-usable Django apps
- Use of unicode in Django
- Django Bi-directional ManyToMany – How to prevent table creation on second model?
- Django 1.6 and django-registration: built-in authentication views not picked up
- ERROR: Invalid HTTP_HOST header: '/webapps/../gunicorn.sock'