[Answered ]-How to import a object of another model (A) inside model (B) in Django?

1👍

After some research and analysis I found solution to my problem, I am posting the solution here so that someone with similar problem could be benefited.

@property
    def get_price(self):
        result = dict()
        variants = ProductVariant.objects.filter(product=self)
        count = variants.count()
        if count > 1:
            min_variant = variants.order_by('price').first()
            max_variant = variants.order_by('-price').first()
            result['min_price'] = min_variant.price
            result['max_price'] = max_variant.price

        elif count == 1:
            variant = variants.first()
            if variant.sale_price:
                result['price'] = variant.price
                result['sale_price'] = variant.sale_price
                sale_variant = variants.order_by('sale_price').first()
                result['lowest_sale_price'] = sale_variant.sale_price
                result['regular_price'] = sale_variant.price
                today = datetime.date.today()
                if variant.sale_start_date <= today and variant.sale_end_date >= today:
                    result['sale_end_date'] = variant.sale_end_date
            else:
                result['price'] = variant.price

Leave a comment