如何将Django模型索引为whoosh

2024-06-26 14:18:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把我的Django模型索引到呼。进来这个tutorial它们只是使用content字段为文本编制索引,但是我如何以这种方式索引Django模型。。。在

我的模型.py在

import json
from django.db import models
from django.contrib import admin
#------------------------------------------------------------------------------ 


class scrapedData (models.Model):
    """ This a model for scraped data collected by eScraper"""

    productMRP = models.FloatField()                                      # Product MRP
    image_urls = models.TextField()                                       # Images URL's for image pipeline for downloading
    productSite = models.URLField()                                       # Product web-site URL
    productDesc = models.TextField()                                      # Product Description
    image_paths = models.TextField()                                      # Product images path on the local machine
    productImage = models.TextField()                                     # Product image URL's
    productTitle = models.TextField()                                     # Product title
    productPrice = models.FloatField()                                    # Product discounted price
    hasVariants = models.BooleanField()                                   # Product variants like : colors or sizes, True is if product has variants, False otherwise
    productCategory = models.TextField()                                  # Product category
    availability = models.BooleanField()                                  # Product availability ,True if product is in stock, False otherwise
    productSubCategory = models.TextField()                               # Product sub-category
    currency = models.CharField(max_length=3)                             # Product price currency
    productURL = models.URLField(max_length=500)                          # Product page URL
    updatedAt = models.DateTimeField(auto_now=True)                       # Time at which product is updated
    createdAt = models.DateTimeField(auto_now_add=True)                   # Time at which product is created


    def product_Image(self):
        """Method to return product images for admin panel"""
        images = ''
        imagePaths = json.loads(self.image_paths)
        for image_path in imagePaths:
            images += '<img src="/images/%s/" height="150" width="150"/>' % image_path            
        return images
    product_Image.allow_tags = True


    def product_URL(self):
        """Method to return product URL for admin panel"""     
        return "<a href=%s>%s<a>"%(self.productURL,self.productTitle)    
    product_URL.allow_tags = True


    class Meta:
        """Meta class to control display Behavior of the Model name """        
        verbose_name_plural = "scrapedData"


class scrapedDataAdmin(admin.ModelAdmin):
    """scrapedData admin class"""

    list_display = ('productTitle','productSite','updatedAt','createdAt',
                    'product_URL','product_Image','productMRP','productPrice','currency',
                    'productDesc','productCategory','availability',
                    'hasVariants','productSubCategory','image_paths','image_urls'
                    )

    search_fields = ('productTitle','productSite','productURL','productMRP',
                    'productPrice','productDesc','productCategory','availability',
                    'hasVariants','productSubCategory','image_paths','image_urls'
                    )

    ordering = ('productSite',)


admin.site.register(scrapedData,scrapedDataAdmin)

#------------------------------------------------------------------------------

我也看过这个tutorial,但我不知道如何将索引存储到Whoosh。我是新来的Whoosh有人能帮我吗。。。在


Tags: imageselftrueurlforadminmodelsproduct