S
S
Sergey Alekseev2018-03-30 15:31:39
Django
Sergey Alekseev, 2018-03-30 15:31:39

Django how to properly return a list of objects?

Good evening, there is a field in the data project model of the JSONField type.

class Project(models.Model):
    name = models.CharField(max_length=30)
    projecttype = models.CharField(max_length=20, choices=TYPE_PROJECT, default=FILM, blank=False)
    cgtype = models.CharField(max_length=20, choices=CG_TYPE, default=REALISTIC, blank=False)
    currency = models.CharField(max_length=11, choices=CURRENCIES, default=USD)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)

    data = JSONField(default=
                     [
                         {
                             "sequences":
                                 [
                                     {
                                         "name": "",
                                         "shots":
                                             [
                                                 {
                                                     "name": ""
                                                 }
                                             ]
                                     }
                                 ]
                         }
                     ]
    )

    def __str__(self):
        return self.name

There is a route
path('api/list_projects/<int:user_id>', ProjectsView.as_view()),

There is a serializer
class ProjectSerializer(serializers.ModelSerializer):
        class Meta:
            model = Project
            fields = ('id', 'customer', 'name', 'projecttype', 'data')

And submission
class ProjectsView(generics.ListAPIView):
    serializer_class = ProjectSerializer

    def get_queryset(self):
        user = self.kwargs["user_id"]
        user_account = UserAccount.objects.get(user=user)
        customer = Customer.objects.get(useraccount=user_account)
        projects = Project.objects.filter(customer=customer)
        return projects

and I get a 500 error when requesting
'JSONField' object has no attribute '_get_val_from_obj'

How to fix this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question