How to retrieve data from different related model (This post was posted earlier on but was deleted due code not being clear)

34 views
Skip to first unread message

Benedict Uwazie

unread,
Oct 24, 2019, 12:40:56 PM10/24/19
to Django users
Please I am new to using google groups. This post have been posted earlier but the
code was not clear enough

I have an issue that I have been battling for a long time I 
want to get data out from different related models. I have three
models. Category, Author, Post. The Category model categories my post 
that are on the Post model. The issue I am having is this, when I perform my loop
on my template (i.e html file) I can get data out from my Post model but I can not
get data out from my Category model. Please I need someone to help me out or tell me
what I am doing wrong.
below is my sample code for you to know what I am talking about.


class Category(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __str__(self):
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

    def __str__(self):
        return self.name

class Post(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, related_name='my_category')
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    authors = models.ManyToManyField(Author)
  

    def __str__(self):
        return self.headline
on views.py
from first_app.models import Author, Category, Post
def post_from_cat(request, cat_id):
b = Category.objects.get(pk=cat_id)
result = b.my_category.all()
return render(request, 'first_app/index.html', {'key':result})

on urls.py
from first_app import views

urlpatterns = [
path('', views.home_app, name='home_app'),
path('post-cat/<int:cat_id>/', views.post_from_cat, name='post_from_cat'),
]



on my template that (my html file)

{% if key %}
{% for k in key %}
<p><strong>Category: </strong> {{ k.name }}</p>
<p><strong>Title</strong><br>{{ k.headline }}</p>
<p><strong>Body</strong><br>{{ k.body_text }}</p>
<hr>
{% endfor %}
{% else %}
<p>No data</p>
{% endif %}





Gil Obradors

unread,
Oct 24, 2019, 2:43:51 PM10/24/19
to django...@googlegroups.com
Hi,

I don't know if I understand what you mean...

Is this good for you?
from first_app.models import Author, Category, Post
def post_from_cat(request, cat_id):
b = Category.objects.get(pk=cat_id)
return render(request, 'first_app/index.html', {'key': b})


Missatge de Benedict Uwazie <nonw...@gmail.com> del dia dj., 24 d’oct. 2019 a les 18:41:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/493caf8b-d3c8-4033-99ab-e01f9eb4ed93%40googlegroups.com.

Benedict Uwazie

unread,
Oct 24, 2019, 3:17:54 PM10/24/19
to Django users
Thanks, but this did not help
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Gil Obradors

unread,
Oct 24, 2019, 3:45:41 PM10/24/19
to django...@googlegroups.com
What field of Category you want to get from post model ? name?
maybe k.category instead of k.name? ( template)

Or explain better :P


Missatge de Benedict Uwazie <nonw...@gmail.com> del dia dj., 24 d’oct. 2019 a les 21:19:
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72e30f40-f636-45fb-88c1-6ac96c5c9c11%40googlegroups.com.

Suraj Thapa FC

unread,
Oct 24, 2019, 10:05:50 PM10/24/19
to django...@googlegroups.com

Benedict Uwazie

unread,
Oct 25, 2019, 3:33:50 AM10/25/19
to Django users
Thank you very much but it did not work

Benedict Uwazie

unread,
Oct 25, 2019, 3:34:45 AM10/25/19
to Django users
Thanks for ur reply, how do I implement it? I tried it but it did not work.


On Friday, 25 October 2019 03:05:50 UTC+1, Suraj Thapa FC wrote:
Try select_related() 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages