Home > Software design >  How to join tables with ManyToManyField type in Django?
How to join tables with ManyToManyField type in Django?

Time:11-18

I have models like this:

class Education(models.Model):
    title = models.CharField(default=None, max_length=100)
    content = models.TextField(default=None)
    price = models.ManyToManyField(Price)

class Price(models.Model):
    cost = models.CharField(default=None, max_length=20)
    created_at = models.DateTimeField(auto_now=True, null=True, blank=True)

And i want to inner join between two tables and access to all fields of both.

CodePudding user response:

We can achieve like this,

Education.objects.filter(price__in=Price.objects.all()).select_related('Price').values_list('title', 'content', 'price__cost', 'price__created_at')

  • Related