S
S
Sergey Alekseev2018-04-02 11:24:25
Django
Sergey Alekseev, 2018-04-02 11:24:25

Django, how to access a field in a queryset?

Good afternoon, there is a simple view

class ProjectsView(View):

    def get(self, request, *args, **kwargs):
        user = kwargs["user_id"]
        user_account = UserAccount.objects.get(user=user)
        customer = Customer.objects.get(useraccount=user_account)
        projects = Project.objects.filter(customer=customer).values()
        return JsonResponse(list(projects), safe=False)

And I need to turn to the QuerySet and extract the attribute of the project (object) from there, let's say data.
How can i do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-04-02
@serj2000

QuerySet is a collection of elements, so your task is to get one element from the collection, and then get the attribute value of this element:

projects = Project.objects.filter(customer=customer).first()
if project is not None:
    data = project.data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question