2017-12-04 1 views
0

나는 봤지만 많은 기사를 읽었지만 여러 테이블에 혼란스러워했습니다. Django가 장고를 사용하여 여러 모델에 합류했습니다. ORM

class ProductCategory(models.Model): 
    category_name = models.CharField(max_length=200,blank=True, null=True, unique=True) 
    category_image = models.ImageField(upload_to='category', null=True, blank=True) 
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True) 
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True) 
    status = models.CharField(max_length=10, default='Active', choices=status) 

    def __unicode__(self): 
     return '%s' % (self.category_name) 

class ProductSubCategory(models.Model): 
    category = models.ForeignKey(ProductCategory) 
    sub_category_name = models.CharField(max_length=200,blank=True, null=True, unique=True) 
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True) 
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True) 
    sub_category_image = models.ImageField(upload_to='subcategory', null=True, blank=True) 
    status = models.CharField(max_length=10, default='Active', choices=status) 

    def __unicode__(self): 
     return '%s' % (self.sub_category_name) 


class Product(models.Model): 
    category = models.ForeignKey(ProductCategory) 
    sub_category = models.ForeignKey(ProductSubCategory) 
    product_name = models.CharField(max_length=200,blank=True, null=True) 
    product_price = models.FloatField(default=0) 
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True) 
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True) 
    # is_discountable = models.CharField(max_length=3, default='Yes', choices=option) 
    status = models.CharField(max_length=10, default='Active', choices=status) 

    def __unicode__(self): 
     return '%s' % (self.product_name) 

class ProductColor(models.Model): 
    product = models.ForeignKey(Product) 
    product_color = models.ForeignKey(Color, related_name='product_color_id', blank=True, null=True) 
    product_size = models.ForeignKey(Size, related_name='product_size_id', blank=True, null=True) 


class ProductImages(models.Model): 
    product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True) 
    product_image = models.ImageField(upload_to='images', null=True, blank=True) 

지금보기에, 나는 카테고리 모든 이미지와 색상을 가진 하위 범주에 따라 제품 필터를 얻고 싶은 '예요 내 모델 보인다. 쿼리는 SQL에 따라서

SELECT product.*, productcolor.*, productimage.* FROM product 
LEFT JOIN productcolor ON productcolor.product_id = product.id 
LEFT JOIN productcolor.product_id = product.id 
LEFT JOIN productimage ON productimage.product_id = product.id 
WHERE product.category_id=1 and product.sub_category_id=1 
+0

귀하의 필요가 광범위하게 보입니다. 당신은 정교 할 수 있습니까? 예제를 제공하십시오. –

+0

@vishes_shell이 ​​방금 질문을 업데이트했습니다. –

+0

@vishes_shell 시도했지만 여전히 convient 솔루션을 얻지 못했습니다 : ( –

답변

-1
ProductCategory.productsubcategory_set.all() 
0

, 이것은 할 것 같은 - 무언가이다.

Product.objects.filter(category=<category>, sub_category=<sub_category>) \ 
    .prefetch_related('productcolor_set', 'productimages_set') 

이 쿼리는이 모든 ProductColorProductImages (.prefetch_related()로) Product에 관한 프리 페치됩니다 <category><sub_category>, 그들은 각각 productcolor_setproductimages_set에 저장된다.

또한 ProductImages 모델의 이름을 ProductImage으로 바꾸려면 하나의 제품 이미지 만 표시해야합니다.

+0

- select_related : 'productimages_set', 'productcolor_set'에 잘못된 필드 이름이 있습니다. 선택 사항은 다음과 같습니다. 카테고리, 하위 카테고리 –

+0

@ V.Khakhil 내 나쁜, 아침 실수, 업데이트 된 답변. –

+0

prefetch_related와 select_related는 모두 조건이있는 제품에서만 선택합니다. 'cart_product','cart_product','cart_product','cart_product''''''''''''''' 'cart_product'.product_description','cart_product'.product_rating','cart_product''created_at','cart_product'.updated_at','cart_product'.status' FROM'cart_product' WHERE ('cart_product'. category_id = 1 AND'cart_product'.sub_category_id' = 1) –

관련 문제