繁体   English   中英

django访问子模型父模型中的外部字段

[英]django access child model foreign field in parent model

我有两个模型,即ParentChild ,我创建了Parent and Child模型的内嵌表单集CRUD。我的内嵌表单集CRUD运行正常。但是问题是,我无法在“父列表视图”中显示Child字段数据。 template。我所做的是,我已经使用@property来获取Child模型数据,但似乎无法正常工作。

django template显示较高级别的数据时,可能有问题,或者可能是在用Parent模型编写的@property出现了问题。

经过长时间的调查,无法解决问题。数据未显示。

我的Parent模型:

class Parent(AbstractBaseModel):

"""
Defining the facets header that will be used during search.
"""

validators = [ChainIntegrityValidator, ScreenMethodValidator]


chain = models.ForeignKey(
        'customers.Chain',
        verbose_name=_('chain')
    )
product_group = models.ForeignKey(
    'products.ProductGroup',
    help_text=_('The product group in which the parent belongs'),
    verbose_name=_('product group')
    )
price = models.BooleanField(
    default=False,
    help_text=_("Identifies whether the 'price' will be included in search facets.\
        Default value false"),
    verbose_name=_('price')
    )
order_price = models.DecimalField(
    max_digits=10,
    null=True,blank=True,
    decimal_places=2,
    help_text=_('Price of a order'),
    verbose_name=_('order price')
    )
monthly_fee = models.BooleanField(
    default=False,
    help_text=_("Identifies whether the 'monthly_fee' will be included in search facets.\
        Default value false"),
    verbose_name=_('monthly fee')
    )
order_monthly_fee = models.IntegerField(
    null=True,blank=True,
    help_text=_('Sorting order of monthly fee.'),
    verbose_name=_('order monthly fee')
    )




def screen_product_group(self):
    if hasattr(self, 'product_group'):
        try:
            obj = Parent.objects.get(chain=self.chain, product_group__name=self.product_group)
        except Parent.DoesNotExist:
            obj = self

        if obj != self:
            return {'product_group': ['A facet header with this product_group already exists']}

@property
def child_attributes_name(self):

    return ','.join([child.attribute.name for child in
                     self.child_set.all()])

class Meta:
    app_label = 'search'
    unique_together = ('chain', 'product_group')
    index_together = [
        ['chain', 'product_group']
    ]

和我的Child模型:

class Child(AbstractBaseModel):

"""
Defining the facets lines that will be used during search.
"""

validators = [ChainIntegrityValidator, ScreenMethodValidator]

chain = models.ForeignKey(
    'customers.Chain',
    verbose_name=_('chain')
    )
parent = models.ForeignKey(
    'search.Parent',
    help_text=_('parent to which child belongs'),
    verbose_name=_('parent')
    )
attribute = models.ForeignKey(
    'products.Attribute',
    help_text=_("attribute to which child belongs"),
    verbose_name=_("attribute"),
    )
order_attribute = models.IntegerField(
    null=True,blank=True,
    help_text=_('Sorting order of attribute'),
    verbose_name=_('order attribute')
    )

class Meta:
    app_label = 'search'
    unique_together = ('chain','facet_header','attribute')
    index_together = [
        ['chain','facet_header','attribute']
    ]

def screen_chain(self):
    if not hasattr(self, 'chain'):
        self.chain = self.facet_header.chain

这是Attribute模型,它是Child键。我想显示“属性”模型中的日期。

class Attribute(AbstractBaseModel):


validators = [ScreenMethodValidator, ChainIntegrityValidator]


attribute_group = models.ForeignKey(AttributeGroup, related_name='attributes')
name = models.CharField(max_length=128,
                        verbose_name=_('name'),
                        help_text=_('Enter Name'))
code = models.SlugField(
    verbose_name=_('Code'),
    help_text=_('Enter Code'),
    max_length=128, )

class Meta:
    app_label = 'products'
    ordering = ['attribute_group', 'code']
    unique_together = ('attribute_group', 'code')
    verbose_name = _('Attribute')
    verbose_name_plural = _('Attributes') 

这是我要显示“属性”模型数据的html模板,这是Child模型中的外键。

    {% for data in object_list %}
                    <tr class="gradeX">
                        <td class="hidden-xs">{{ data.product_group }} </td>

                        <td class="hidden-xs">{{ data.price }}</td>
                        <td class="hidden-xs">{{ data.order_price }}</td>
                        <td class="hidden-xs">{{ data.monthly_fee }}</td>
                        <td class="hidden-xs">{{ data.order_monthly_fee }}</td>


                        <td class="hidden-xs">
                            <dl class="dl-horizontal">
                                {% for child in data.child_set.all.all %}
                                    <dt>{{ child.attribute.attribute_group.name }}</dt>
                                    <dd>{{ child.attribute.name }}</dd>
                                {% endfor %}
                            </dl>
                        </td>

                {% endfor %}

更新

class FacetsList(WammuListView):
    template_name = 'dashboard/parent_list.html'
    model = Parent

def get_queryset(self):
    chain = Chain.objects.filter(id=self.kwargs['chain_pk'])
    return Parent.objects.filter(chain=chain)

在模板中

{% for child in data.child_set.all.all %}

第二个.all可能引起此问题,child_set是一个查询集,.all应该足够。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM