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
from first_app.models import Author, Category, Postdef 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})
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'),]
{% 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 %}
from first_app.models import Author, Category, Postdef post_from_cat(request, cat_id): b = Category.objects.get(pk=cat_id)
return render(request, 'first_app/index.html', {'key': b})
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK-JoTTSCEcaOr-uZvR_VXBgqRaS_xVO0h6htHsD7yMRSrGM%3DA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72e30f40-f636-45fb-88c1-6ac96c5c9c11%40googlegroups.com.
Try select_related()
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72e30f40-f636-45fb-88c1-6ac96c5c9c11%40googlegroups.com.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK-JoTTSCEcaOr-uZvR_VXBgqRaS_xVO0h6htHsD7yMRSrGM%3DA%40mail.gmail.com.