1👍
You should use ManyToManyField
actually:
class Posts(models.Model):
title = models.CharField(max_length=155)
summary = models.TextField(blank=True, null=True)
slug = models.SlugField(unique=True, max_length=155)
class Recipes(models.Model):
title = models.CharField(max_length=155)
summary = models.TextField(blank=True, null=True)
content = models.TextField(blank=True, null=True)
posts = mdoels.ManyToManyField("Posts", related_name='recipes')
Django docs example : https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_many/
After that, you can access related table via:
Recipes.objects.first().posts # first recipe for example
Posts.objects.first().recipes # first post for example, accessing via related name
And in your template would be sth like this:
{% for item in recipes %}
<a href="{{ STATIC_URL }}/{{ item.posts.first.slug }}">{{ item.posts.first.title }}</a>
{% endfor %}
👤Amin
Source:stackexchange.com