I am developing a backend for a landing page type site, and during the development of the project I came across a lot of models
...the rest of the code
class FunctionalBlock(models.Model):
title = models.CharField(max_length=255,blank=True, null=True)
subtitle = models.CharField(max_length=255,blank=True, null=True)
icon = models.ForeignKey(to='Image', on_delete=models.SET_NULL, blank=True, null=True,related_name='function_block_icon')
image = models.ForeignKey(to='Image', on_delete=models.SET_NULL, blank=True, null=True,related_name='function_block_image')
form = models.ForeignKey(to='FormNVA', on_delete=models.SET_NULL, blank=True, null=True,related_name='function_block')
buttons = models.ManyToManyField(to='Buttons',blank=True, null=True)
time = models.DateTimeField(blank=True, null=True)
section = models.ForeignKey(to='Section', on_delete=models.CASCADE,related_name='function_block')
text = models.TextField(blank=True, null=True)
class Section(models.Model):
title = models.CharField(max_length=255, blank=True, null=True)
subtitle = models.CharField(max_length=255, blank=True, null=True)
background = models.ForeignKey(to='Image', on_delete=models.SET_NULL, blank=True, null=True,related_name='section')
# PAGE
class PageHome(models.Model):
title = models.CharField(max_length=255)
language = models.ForeignKey(to='Language', on_delete=models.SET_NULL, blank=True, null=True)
head = models.OneToOneField(to='Head', on_delete=models.SET_NULL, blank=True, null=True)
body = models.OneToOneField(to='Body', on_delete=models.SET_NULL, blank=True, null=True, related_name='page_home')
objects = models.Manager()
objects_lang = LanguageManager()
def __str__(self):
return f'{self.title}'
class PageServices(models.Model):
title = models.CharField(max_length=255)
language = models.ForeignKey(to='Language', on_delete=models.SET_NULL, blank=True, null=True)
head = models.OneToOneField(to='Head', on_delete=models.SET_NULL, blank=True, null=True)
body = models.OneToOneField(to='Body', on_delete=models.SET_NULL, blank=True, null=True, related_name='page_services')
objects = models.Manager()
objects_lang = LanguageManager()
class PageCareer(models.Model):
title = models.CharField(max_length=255)
language = models.ForeignKey(to='Language', on_delete=models.SET_NULL, blank=True, null=True)
head = models.OneToOneField(to='Head', on_delete=models.SET_NULL, blank=True, null=True)
body = models.OneToOneField(to='Body', on_delete=models.SET_NULL, blank=True, null=True, related_name='page_career')
objects = models.Manager()
objects_lang = LanguageManager()
# BODY
class Body(models.Model):
class FieldSets(Enum):
HOME =['services','options','become_part_of_us','cta_text','numbers','partners','questions']
SERVICES = ['services']
# Home
services = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_services')
options = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_options')
become_part_of_us = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_become_part_of_us')
cta_text = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_cta_text')
numbers = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_numbers')
partners = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_partners')
questions = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_questions')
# Services
news = models.ForeignKey(to='Section',on_delete=models.SET_NULL, blank=True, null=True, related_name='body_home_news')
as you can see I have a separate model for each page, similarly I had a separate Body Model for each page model and to have them reduce I have summarized the fields in one table namely body and here I am facing a problem which is when I create any page in admin panel I want to filter the fields specifically related to this page model when I click on add body in the admin panel and a popup appears
ADMIN PANEL
as a solution I looked at passing url params and using get_fieldsets to filter the fields but I couldn’t achieve what I wanted.
@admin.register(Body)
class BodyAdmin(admin.ModelAdmin):
def get_fieldsets(self, request, obj):
model = self.model
related_model = self.opts.related_objects[0].related_model
fieldsets = copy.deepcopy(super().get_fieldsets(request, obj))
if related_model is PageHome:
fieldsets[0][1]['fields'] = model.FieldSets.HOME.value
if related_model is PageServices:
fieldsets[0][1]['fields'] = model.FieldSets.SERVICES.value
return fieldsets
Is my approach to creating a backend for the landing page correct and if so, is it possible to achieve field filtering in the body?
thanks in advance
I tried passing query params to solve this problem, I also looked for a dependency in BodyAdmin to the calling Page…Admin but these two options were unsuccessful