2👍
✅
You don’t need that intermediate table in your models, django will build that for you behind the scenes.
#models.py
class PaymentProcessor(models.Model):
name = models.CharField(max_length=75)
def __str__(self):
return self.name
class Site(models.Model):
name = models.CharField(max_length=75)
link = models.CharField(max_length=150)
description = models.TextField(blank=True, null=True)
payment_processors = models.ManyToManyField(PaymentProcessor, null=True, blank=True)
def __str__(self):
return self.name
And then in the view you just need to get the site_list, because you can get the payment processors from the site object:
#views.py
def home(request):
site_list = Site.objects.all()
return render(request, 'index.html',
{'site_list': site_list})
Finally in the template you loop through the ManyToMany object with the all function, and there ya have it!
#index.html
{% for site in site_list %}
{{ site }}:
{% for p in site.payment_processors.all %}
{{ p }}
{% endfor %}
{% endfor %}
Source:stackexchange.com