How to retrive data from more than two tables with many to many relationships?

Skip to first unread message

Madhav Dhungana

Nov 12, 2021, 1:38:09 PM11/12/21
to Django users

What I want?

1 ) I want to add pages and pull the page meta data from pagemeta table. One page will have more than on pagemeta.

  1. PageMeta Table , will have section(many to many relationship) and key and value like: Keyname: _contact_collapse Value: People usually ask these

  2. Section Table name: _contact_us_fax

  3. ModelsMeta Table This table will have: sectionname( foreign key) model_key : choices , for example: _heading_title, _heading_paragraph meta_vale ( this is for charfield) meta_content: HTML Field

What I want to achieve from this? I want to make my own kind of simple CMS where I can create dynamic pages and assign fields to pages by establishing relationship with other tables via many to many or foreign key relationship when required.

Whats the problem?

To publish the specific page content, I need to know the pagemeta id's of that page from that pagemeta table id's, I need to find section table related to each and from the section id I will need to find information from ModelsMeta table. From this I hope I will be able to create a cms system where I can call the other tables data from keyvalue pair.

My table structure code:

class SectionKey(models.Model):

model_name =models.CharField(max_length=200)

class Meta:

verbose_name = "Section"

verbose_name_plural = "Section"

def __str__(self):

return self.model_name

class Models(models.Model):

model_name =models.CharField(max_length=200)

class Meta:

verbose_name = "Models"

verbose_name_plural = "Models"

def __str__(self):

return self.model_name

class ModelsMeta(models.Model):

model_meta_name =models.ForeignKey(SectionKey, on_delete= models.CASCADE,related_name='sectionKey')

modelkey = models.ForeignKey(Models, on_delete= models.CASCADE,related_name='Models', default=0)

meta_value = models.CharField(max_length=200)

meta_content = HTMLField()

class Meta:

verbose_name = "Models Meta"

verbose_name_plural = "Models Meta"

def __str__(self):

return self.meta_value

class PageMeta(models.Model):

section = models.ManyToManyField(SectionKey)

name = models.CharField(max_length=500, blank=True)

title = models.CharField(max_length=500, blank=True)



class Meta:

verbose_name = "Page Meta"

verbose_name_plural = "Page Meta"

def __str__(self):


class Page(models.Model):


title = models.CharField(max_length=500)

content = models.CharField(max_length=500)

author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='page')

updated_on = models.DateTimeField(auto_now= True)

content = HTMLField()

page_meta = models.ManyToManyField(PageMeta)

created_on = models.DateTimeField(auto_now_add=True)

slug = models.SlugField(max_length=200, unique=True, default=0)

status = models.IntegerField(choices=STATUS, default=0)

class Meta:

verbose_name = "Page"

verbose_name_plural = "Pages"

def __str__(self):

return self.title

What I have done:

def contact(request, slug):

page = get_object_or_404(Page, slug=slug)

meta = page.page_meta.values_list('id', flat=True)

page_meta = Page.objects.filter(id__in=meta)

section = PageMeta.objects.prefetch_related('section', id__in=meta)

models = ModelsMeta.objects.prefetch_related('section', id__in=section)

I tried to achieve the goal with this way but I am unable to move ahead and get required information on managed way. I can get the page meta id of each page but now, how can I get the section of each (desired) pagemeta, and how can I get the modelsmeta id of each section obtained from pagemeta?

Long story short, I want to retrieve Page model related data from all the tables and print on template for each page.

Lalit Suthar

Nov 21, 2021, 1:43:35 AM11/21/21

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
To view this discussion on the web visit
Reply all
Reply to author
0 new messages