Search code examples
python-3.xdjangomultidimensional-arraydjango-templatesdjango-queryset

Django Python : Manager isn't accessible via model instances


I'm trying to query an object set and use the number of the amount of objects to do a for loop that will present parameters of each object. I'm using a 2D array to store the parameters so they can be presented respective to their object. I would like not just a solution but a better way in general of doing this .p.s. I'm new to django

This is the code from views.py

def flashcard(response):
    form = Flashcard_verif(response.POST)

    if response.method == "POST" and form.is_valid():
          
        head = form.cleaned_data["header"]
        ques = form.cleaned_data["question"]
        ans = form.cleaned_data["answer"]
            
        flashcard_data = Flashcard(header=head, question=ques, answer=ans)


        # this is where the error is occuring
        flashcard_obj = Flashcard(header=head, question=ques, answer=ans).objects.all()

        rawHeader, rawQuestion, rawAnswer = Flashcard(header=head, question=ques, answer=ans).flash_format()
        
        flashcard_data.save()

        if len(flashcard_lib) > 1:
            for i in range(0, len(flashcard_lib), 1):
                flashcard_lib.append([rawHeader, rawQuestion, rawAnswer])
            
            print(flashcard_lib)
        
            return render(response, "main/Flashcard.html", {
            "form":form,
            "Flashcard_lib":flashcard_lib,
            "flashcard":flashcard_obj})
        else:
            flashcard_lib.append([rawHeader, rawQuestion, rawAnswer])
            print(flashcard_lib)
            
            return render(response, "main/Flashcard.html", {
            "form":form,
            "header":rawHeader, 
            "question":rawQuestion, 
            "answer":rawAnswer
            })
        

    
    else:
        form = Flashcard_verif()
    
    return render(response, "main/Flashcard.html", {
        "form":form})

This is my template that utilises the 2d array:

<div class="flashcard-view" id="flash" style="visibility: hidden">
    {% if Flashcard_lib|length >= 2 %}
        {% for i in flashcard_obj %}
            <div class="flashcard">
            {{ i.0 }}
            {{ i.1 }}
            {{ i.2 }}
            </div>
        {% endfor %}
    {% else %}
        <div class="flashcard">
        {{ header }}
        {{ question }}
        {{ answer }}
        </div>
    {% endif %}
</div>

When i run the server I notice that the if statement executes but the for loop does not. This is kind've a 2 in 1 problem, how do i get it to iterate through the list.


Solution

  • In line

    flashcard_data = Flashcard(header=head, question=ques, answer=ans)
    

    You created instance of model Flashcard, but you didnt saved it to db, to do so you need to do:

    flashcard_data.save()
    

    An error occured in line

    flashcard_obj = Flashcard(header=head, question=ques, answer=ans).objects.all()
    

    because you created another instance (with same data as instance 2 lines above) and then you try to get obj manager with it. In Django model instances can be represented as:

    • single objects (as in your case)
    • As querysets - special list-like structures, empty or containing one or several instances.

    If you want to get all Flashcard records in db you need to do

    Flashcard.objects.all()
    

    Where Flashcard - is your model, objects - is object manager, that will let you to specify what do you need - Flashcard.objects.all() - returns queryset with all instases Flashcard.objects.filter(...) - - returns queryset with filtered records Flashcard.objects.first() - returns first object (not queryset!) in db etc..

    You cant call obj manager from instance