dabapps / django-readers

A lightweight function-oriented toolkit for better organisation of business logic and efficient selection and projection of data in Django projects.
https://www.django-readers.org
BSD 2-Clause "Simplified" License
183 stars 7 forks source link

I have this error and not sure how to solve AttributeError: 'ProductPackage' object has no attribute 'only' #60

Closed simkimsia closed 2 years ago

simkimsia commented 2 years ago

this is my code

from django_readers import specs
def get_product_package_details_by_id(pk):
    """
    get product package by id for details
    """
    spec = [
        "id",
        "name",
        {
            "product_package_line_items": [
                {
                    "product": [
                        "description_in_mus",
                        "description_sow",
                        "id",
                        "product_number",
                        "unit",
                    ]

                },
                "quantity"
            ]
        },
    ]

    prepare, _ = specs.process(spec)
    return prepare(ProductPackage.objects.get(pk=pk))

product_package = get_product_package_details_by_id(pk)

then I get AttributeError: 'ProductPackage' object has no attribute 'only'

j4mie commented 2 years ago
return prepare(ProductPackage.objects.get(pk=pk))

prepare has to be called on a queryset, not a single object. Try:

return prepare(ProductPackage.objects.all).get(pk=pk)
simkimsia commented 2 years ago

Thanks @j4mie

Is everything okay? i see the badge as failing.

j4mie commented 2 years ago

@simkimsia I'll open a ticket for that, the badge just seems to show the status of whatever the most recent build was, so the reason it's failing is because of this PR. Thanks!