Django主子表自定义

Django主子表自定义,django,Django,我有主表到子表,需要一个解决方案来呈现它与以下条件 Master Table Fields:- Person product categ price image field1 field2 Child Table (User customised):- User Product categ customprice customfield1 customfield2 Query:- totalrecords = Master.objects.filter(Person=person1).fi

我有主表到子表,需要一个解决方案来呈现它与以下条件

Master Table Fields:-
Person product categ price image field1 field2


Child Table (User customised):-
User Product categ customprice customfield1 customfield2

Query:-
totalrecords = Master.objects.filter(Person=person1).filter(categ=catogory1)
enabledrecords = Child.objects.filter(user=user).filter(categ=categ1)
子项中的产品是来自主项的外键。 在模板中,我将提取主字段(在for循环中),但如果price和customfields存在于child中,那么我需要使用产品关系获取子对象,否则从主表中填充它们

困惑来了

{% for obj in totalrecords %}
     if obj.id in enabledrecords (using product forign key) then 
     Get en_obj from enabledrecords__product
     {{obj.id}} {{en_obj.id}} {%if en_obj.customprice%} {{en_obj.customprice}}
     {%else%}{%obj.price%}{%endif%} -->do same for other customfields

     if obj.id not in enabledrecords 
     {{ obj.id }} <p> Product not customized click here to customise </p>

子表可能包含或不包含给定供应商和pdctg的所有主数据行,为什么不为您提到的表添加models.py代码?这将帮助我们更快地调试!嗨Abhijith我加了谢谢
Products(Master Table):-
vendor = models.ForeignKey(Shop,verbose_name='Shop Name')
name     =  models.CharField('Product Name', max_length=100 )
pfimage           =  models.ImageField('Image', upload_to='pd/%Y',)
pdctg        =  models.ForeignKey(PdCtg, null=True, blank=True, verbose_name='Product Category')
mrp         =models.DecimalField('MRP (optional)',max_digits=6, decimal_places=2,  null=True, blank=True)
ourprice     =models.DecimalField('Our Price (optional)',max_digits=6, decimal_places=2, null=True, blank=True)
offer       =models.CharField('Offers (optional)',max_length=10, null=True, blank=True)

Child Table:
vendor        =models.ForeignKey(Shop,verbose_name='Shop Name')
pdctg   = models.ForeignKey(PdCtg,null=True, blank=True,verbose_name='Product Category')
products     =models.ForeignKey(Products,  verbose_name='Product Name') 
pdid        =models.CharField('Item ID',max_length=100, null=True, blank=True)
mrp         =models.DecimalField('MRP (optional)',max_digits=6, decimal_places=2, null=True, blank=True)
ourprice     =models.DecimalField('Our Price (optional)',max_digits=6, decimal_places=2, null=True, blank=True)
offer       =models.CharField('Offers (optional)',max_length=10, null=True, blank=True)