2017-06-26 19 views
9

के साथ संबंधित ऑब्जेक्ट्स को इंडेक्स करना और खोजना मैं कार्यान्वयन खोजने के लिए बहुत नया हूं, जबकि मैं सीख रहा हूं, मेरे साथ भालू!हैस्टैक

तो मेरी पालतू परियोजना एक नुस्खा साइट है और प्रत्येक नुस्खा में एन कदम हो सकते हैं।

class Recipe(models.Model): 
    title = models.CharField(max_length=255) 
    description = models.TextField() 
    hotness = models.ForeignKey(Hotness) 
    recipe_diet = models.ManyToManyField(DietType) 
    ingredients = models.ManyToManyField(Ingredient, through="RecipeIngredient") 

class DietType(models.Model): 
    diet_type = models.CharField(max_length=50) 
    description = models.TextField(null=True, blank=True) 

class RecipeIngredient(models.Model): 
    recipe = models.ForeignKey(Recipe) 
    ingredient = models.ForeignKey(Ingredient) 
    quantifier = models.ForeignKey(Quantifier) 
    quantity = models.FloatField() 

class RecipeSteps(models.Model): 
    step_number = models.IntegerField() 
    description = models.TextField() 
    recipe = models.ForeignKey(Recipe) 

(संक्षिप्तता के लिए संक्षिप्त)

मैं इसके बारे में सभी अनुक्रमित करना चाहते हैं: पकाने की विधि, RecipeIngredient, DietType और कदम ... DietType और RecipeIngredient ठीक काम कर रहा हो रहे मॉडल की तरह कुछ लग रहा है , लेकिन कदम नहीं हैं। मुझे लगता है कि इसे 'relatedSearchQuerySet' के उपयोग से करना है? सब कुछ काम करता है, को छोड़कर -

{{ object.title }} 
{{ object.cuisine }} 
{% for ingr in object.ingredients.all %} 
    {{ ingr.title }} 
{% endfor %} 
{{ object.description }} 
{% for dt in object.recipe_diet.all %} 
    {{ dt.diet_type }} 
{% endfor %} 
{{ object.user }} 
{{ object.hotness }} 
{% for step in object.recipesteps.all %} 
    {{ step.description }} 
{% endfor %} 
{{ object.body }} 

मैं सामग्री, शीर्षक, विवरण, आहार प्रकार खोज सकते हैं:

from haystack import indexes 
from recipes.models import Recipe 

class RecipeIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True) 
    title = indexes.CharField(model_attr='title') 
    ingredients = indexes.MultiValueField(indexed=True, stored=True) 
    description = indexes.CharField(model_attr='description') 
    hotness = indexes.CharField(model_attr='hotness') 
    diet_type = indexes.MultiValueField(indexed=True, stored=True) 
    recipesteps = indexes.MultiValueField(indexed=True, stored=True) 

    def prepare_steps(self, object): 
     return [step.description for step in object.recipesteps.all()] 

    def get_model(self): 
     return Recipe 

    def load_all_queryset(self): 
     # Pull all objects related to the Note in search results. 
     return Recipe.objects.all().select_related() 

यहाँ टेम्पलेट recipe_text.txt है:

यहाँ मेरी search_indexes.py है RecipeSteps।

अंत में, मैं केवल इस समय खोल के माध्यम से प्रश्नों बना रही हूँ:

#producing results: 
sq = SearchQuerySet().filter(content='onion') #ingredient 
sq = SearchQuerySet().filter(content='bolognese') #title 
sq = SearchQuerySet().filter(content='bologna') #description 
#not producing any results: 
sq = SearchQuerySet().filter(content='chop') #step 
sq = RelatedSearchQuerySet().filter(content='chop').load_all() #assuming this does the expanded search 

किसी भी विचार?

+0

मैं व्यंजनों है कि एक कदम है कि एक खोज क्वेरी मेल खाता हो प्राप्त करने में सक्षम होना चाहता हूँ। आईई: मैं खुद को कदम खोजना चाहता हूं लेकिन नुस्खा वापस लेना चाहता हूं। – dengar81

+0

क्या 'पकाने की विधि' की अपनी अनुक्रमणिका है? –

+0

नहीं, मैंने अभी तक यह खोज के लिए अनुक्रमित नहीं किया है। क्या वह कुंजी है? – dengar81

उत्तर

5

मैं दो मुद्दों की पहचान की है: अपने RecipeIndex में

  1. नाम prepare_steps गलत है यह होना चाहिए prepare_{field_name} तो prepare_recipesteps

  2. के लिए इसे बदल आप संबंधित चरणों object.recipesteps.all में recipe_text.txt वस्तुओं का उपयोग करने की कोशिश कर रहे हैं एक गलत तरीका, यह object.recipesteps_set.all होना चाहिए। या recipesteps का उपयोग करते रहें, लेकिन इसे RecipeStepsForeignKey पकाने की विधि के लिए related_name के रूप में जोड़ें।

    class RecipeSteps(models.Model): 
        # // 
        recipe = models.ForeignKey(Recipe, related_name='recipesteps') 
    
संबंधित मुद्दे