Home > Mobile >  how to fetch data from a list of dictionary using django
how to fetch data from a list of dictionary using django

Time:03-22

I am trying to develop a dictionary(using django) that contains a voice for pronunciation. everything goes fine but I cannot relate the audio with the word using the following query.

Model.py

class Warehouse(models.Model):

    word                    = models.CharField(max_length=200, blank=True)
    type                    = models.CharField(max_length=200, blank=True)
    gender                  = models.CharField(max_length=200, blank=True)
    meaning                 = models.TextField(max_length=2000, blank=True)
    synonyms                = models.CharField(max_length=200, blank=True)
    antonyms                = models.CharField(max_length=200, blank=True)
    usage                   = models.TextField(max_length=2000, blank=True)
    


class Audio(models.Model):

    warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
    audio_file = models.FileField(upload_to='audio')

    def __str__(self):
        return self.warehouse.word
    def delete_media(self):
        os.remove(path=MEDIA_ROOT   '/'   str(self.audio_file))

query.py

def get_words_warehouse_matching(search_sting):
    try:
        dictionary_object = Warehouse.objects.filter(Q(word__icontains = search_sting)|Q(type__icontains=search_sting) | Q(gender__icontains=search_sting)|Q(synonyms__icontains=search_sting)|Q(antonyms__icontains=search_sting)
        words_list = []
        for words in dictionary_object.iterator():
            word_dictionary = {'id':words.id, 'word': words.word, 'meaning': words.meaning,     'synonyms': words.synonyms,'antonyms': words.antonyms}    
            words_list.append(word_dictionary)      
        return words_list

                   

views.py

def search(request):
context = {}
warehouse={}
if request.method == 'POST':
    context['data'] = get_words_by_matching(request.POST['searchString'])
    context['warehouse'] = get_words_warehouse_matching(request.POST['searchString'])
    voice = Audio.objects.filter(warehouse_id = warehouse.id)
    context['audio'] = voice
return render(request, 'dictionary/word.html', context)

I need to display the related audio file in the template so that I need to equate warehouse_id to Warehouse.id. How can I fetch the id from get_words_warehouse_matching (dictionary) so that I could use
filter(warehouse_id = warehouse.id) in views. Or if there are any other better options, suggestions would be highly appreciated. Thanks in advance.

CodePudding user response:

It seems you just need to query on your Audio model.

q = Q(warehouse__word__icontains = search_sting)|Q(warehouse__type__icontains=search_sting) | Q(warehouse__gender__icontains=search_sting)|Q(warehouse__synonyms__icontains=search_sting)|Q(warehouse__antonyms__icontains=search_sting)
context['audio'] = Audio.objects.filter(q)

And then you can access these data in template like below:

{% for aud in audio %}
    {{ aud.warehouse.id }}
    {{ aud.warehouse.word }}
    {{ aud.warehouse.meaning }}
    ....
{% endfor %}

CodePudding user response:

I have solved this issue by modifying the query as:

context = {}
    if request.method == 'POST':
        context['data'] = get_words_by_matching(request.POST['searchString'])
        warehouse_val = get_words_warehouse_matching(request.POST['searchString'])
        id=warehouse_val[0]['id']
        context['warehouse'] = get_words_warehouse_matching(request.POST['searchString'])
        context['audio'] = Audio.objects.filter(warehouse_id=id)

    return render(request, 'dictionary/word.html', context)

Can someone please suggest to me if this is the better solution or I can do something else so that my query will be more efficient. Thanks

  • Related