Sunday, 24 April 2022

How to count a single field of a django queryset with multiple group by?

Let's say I have a queryset qs. I'm grouping by the queyset as follow:

(
    qs.annotate(
        catering_price_enabled=F("outlet__library__settings__sligro_catering_price_enabled"),
    )
    .values("assortment_sync_id", "catering_price_enabled")
    .order_by("assortment_sync_id", "catering_price_enabled")
    .distinct("assortment_sync_id", "catering_price_enabled")
)

And I'm getting something like:

<QuerySet [
   {'assortment_sync_id': '01234', 'catering_price_enabled': False}, 
   {'assortment_sync_id': '01234', 'catering_price_enabled': None}, 
   {'assortment_sync_id': '56789', 'catering_price_enabled': None},
]>

What I'm trying to do is to annotate this queryset so I can eventually filter for values > 1. In other words, each assortment_sync_id can have only value of catering_price_enabled.

If I add .annotate(count=Count("assortment_sync_id")) django raises NotImplementedError: annotate() + distinct(fields) is not implemented. I tried this approach because it obviously works with just one field.

How can I get the expected output below?

<QuerySet [
   {'assortment_sync_id': '01234', 'catering_price_enabled': False, 'count': 2}, 
   {'assortment_sync_id': '01234', 'catering_price_enabled': None, 'count': 2}, 
   {'assortment_sync_id': '56789', 'catering_price_enabled': None, 'count': 1},
]>


from How to count a single field of a django queryset with multiple group by?

Python module implemented like a package

I have an application containing a package, thus:

* fruits/
  | citrus/
  | | __init__.py
  | | oranges.py
  | | mandarins.py
  | | satsumas.py
  | | ... etc

The Python files under fruits/citrus/ contain definitions of about 300 subclasses of fruits.citrus.Citrus.

They live in separate files like this only for administrative reasons. Nobody should know or care about fruits.citrus.mandarins etc unless they're working on the fruits.citrus package. To put it another way, I would like this package to pretend it's a module.

I have tried putting this in __init__.py:

from fruits.citrus.oranges import *
from fruits.citrus.mandarins import *
from fruits.citrus.satsumas import *
[...]

g = list(globals().items())

__all__ = list([
    name for name, value in g
    if value.__class__==type and
    issubclass(value, Citrus)
    ])

This sort of works, in that you can see fruits.citrus.Tangerine. But pydoc and sphinx still list oranges, mandarins, etc as package contents. And Tangerine is both at fruits.citrus.Tangerine and at fruits.citrus.mandarins.Tangerine, so things are even more complicated than when I started.

There has to be a way to do this, but everything I tried has been a lemon. Help?



from Python module implemented like a package

How to expose any API globally on Node

I have a web development framework which I use on personal backend projects. All web apps revolve around my framework and the API is quite big so importing it in each file becomes tiring and repetitive.

The solution I came up with is using this code in my program main/entry point

Object.assign(global, require("@mycompany/myframework"));

Now I'm able to access all classes, constants and functions without importing them in each file.

The only problem is that my framework is written in TypeScript but my IDE is not recognising the types globally, so after using any of my framework class, the editor/IDE will report that the API has not been imported.

Do you have a complete solution for this problem?



from How to expose any API globally on Node