django两个多对多领域之间的关系

时间:2016-03-27 06:26:19

标签: python django

我的应用程序有以下模型结构。我有以下模型,

  1. 产品
  2. ProductGroup
  3. 属性
  4. 目前我正在开发另外两个模型(which are some how related with the above three models),即

    1. FacetHeader
    2. FacetLines
    3. 我所做的是,i have to find a Attribute from a particular ProductGroup其中AttributeForeignKey模型中的FacetLinesProductGroup ForeignKey中的FacetHeader Attribute 1}}。

      提及 ProductGroupManyToManyProduct模型的关系也class FacetHeader(AbstractBaseModel): """ Defining the facets header that will be used during search. """ validators = [ChainIntegrityValidator, ScreenMethodValidator] product_group = models.ForeignKey( 'products.ProductGroup', help_text=_('The product group in which the facet header belongs'), verbose_name=_('product group') )

      但这对我来说真的很难,因为我是Djang0的新人,我的模型结构对我来说有点复杂。所以,请看看我的模型结构,(这里我顺序代表我的模型)

      FacetHeader

       class FacetLines(AbstractBaseModel):
      
          """
          Defining the facets lines that will be used during search.
          """
      
          validators = [ChainIntegrityValidator, ScreenMethodValidator]
      
          facet_header = models.ForeignKey(
              'search.FacetHeader',
              help_text=_('facet header to which facet lines belongs'),
              verbose_name=_('facet header')
              )
          attribute = models.ForeignKey(
              'products.Attribute',
              help_text=_("attribute to which facet lines belongs"),
              verbose_name=_("attribute"),
              )
      

      FacetLines

      class Product(AbstractBaseModel):
          """
          Describes product. products are basically parent  product that does not
          have a sku. For example, a product can be an Iphone 5, but the sku are the variants which
          can be something like Iphone 5 Black, Iphone 5 Grey, etc.
          """
      
          product_group = models.ManyToManyField(ProductGroup,
                                             blank=True)
      
          attributes = models.ManyToManyField(Attribute,
                                          blank=True,
                                          verbose_name=_("attributes"),
                                          help_text=_('Select Attributes of  product'),
                                          through='ProductAttribute')
      

      产品

      class ProductAttribute(AbstractAttribute):
          """
          This is the through models that holds attribute value
          for products
      
          """
      
          validators = [ScreenMethodValidator, ChainIntegrityValidator]
      
          product = models.ForeignKey(Product,
                                  related_name='attribute_values',
                                  verbose_name=_("Product"))
          value_option = models.ManyToManyField(Option,
                                            blank=True,
                                            null=True,
                                            verbose_name=_("Option"))
      

      这是 ProductAttribute 模型

      Attributes

      那么如何使用ProductGroupFacetHeader模型获取特定FacetLines(即product_group_pk = 1)的所有FacetHeader

      更新 在这里,我使用内嵌表单集创建FacetLines多个FacetHeader,其中parentFacetLines表单,when i select a particular product group from FacetHeader model是子表单。

      我真正需要做的是,

      FacetLines forms,there should be appear attribute of that selected product group of FacetHeader model然后在// This code is placed in the public domain struct AllocatedMemory<'a> { mem : &'a mut [u8], next : Option<&'a mut AllocatedMemory <'a> >, } struct Alloc<'a> { glob : Option<&'a mut AllocatedMemory <'a> >, } impl<'a> Alloc <'a> { fn alloc_cell(self : &mut Alloc<'a>) -> &'a mut AllocatedMemory<'a> { match self.glob { Some(ref mut glob_next) => { let rest : Option<&'a mut AllocatedMemory <'a> >; match glob_next.next { Some(ref mut root_cell) => { rest = std::mem::replace(&mut root_cell.next, None); }, None => rest = None, } match std::mem::replace(&mut glob_next.next, rest) { Some(mut root_cell) => { return root_cell; }, None => panic!("OOM"), } }, None => panic!("Allocator not initialized"), } } fn free_cell(self : &mut Alloc<'a>, mut val : & 'a mut AllocatedMemory<'a>) { match self.glob { Some(ref mut glob_next) => { match std::mem::replace(&mut glob_next.next ,None) { Some(mut x) => { let _discard = std::mem::replace(&mut val.next, Some(x)); }, None => {}, } glob_next.next = Some(val); }, None => panic!("Allocator not initialized"), } } } struct AllocatorGlobalState<'a>{ cell1 : AllocatedMemory<'a>, cell0 : AllocatedMemory<'a>, sentinel : AllocatedMemory<'a>, allocator :Alloc<'a>, } fn main() { let mut buffer0 : [u8; 1024] = [0; 1024]; let mut buffer1 : [u8; 1024] = [0; 1024]; let mut sentinel_buffer : [u8; 1] = [0]; let mut ags : AllocatorGlobalState = AllocatorGlobalState { cell1 : AllocatedMemory{mem: &mut buffer1[0..1024], next: None}, cell0 : AllocatedMemory{mem: &mut buffer0[0..1024], next: None}, sentinel : AllocatedMemory{mem: &mut sentinel_buffer[0..1], next: None}, allocator : Alloc {glob : None}, }; ags.allocator.glob = Some(&mut ags.sentinel); ags.allocator.free_cell(&mut ags.cell1); ags.allocator.free_cell(&mut ags.cell0); { let mut x = ags.allocator.alloc_cell(); x.mem[0] = 4; let mut y = ags.allocator.alloc_cell(); y.mem[0] = 4; ags.allocator.free_cell(y); let mut z = ags.allocator.alloc_cell(); z.mem[0] = 8; //y.mem[0] = 5; // <-- this is an error (use after free) } }

0 个答案:

没有答案