#ManyToManyField lookup which Django seems doesn't support yet

37 messages ยท Page 1 of 1 (latest)

woeful lantern
#

I want to do something which it seems Django doesn't officially supports it yet.

Here goes the models.py

class Report(models.Model):
    # basic ID with models.AutoField
    pass
class BulkOfReports(models.Model):
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid7,
        editable=False,
        verbose_name="ID",
    )
    reports = models.ManyToManyField(
        Report,
        verbose_name=_("laporan-laporan"),
        related_name="bulk_of_reports",
    )

And for example if those BulkOfReports can be represented like this:

BulkOfReports = [Reports_ids]

Then with data like this:

A = [1, 2, 3]
B = [1, 2]
C = [1]
D = [2]
E = [3]

How could I get the A option only with Django's QuerySet API?

I have tried this:

BulkOfReports.reports.filter(id__in=[1, 2, 3])

But what I have is all of those QuerySet mentioned above:

A, B, C, D, E
hollow sail
#

Have you tried not having any lookup parameters?

So id=...?

woeful lantern
#

Do you mean to chain the filters?

hollow sail
#

I would be experimenting in a shell at this point if I was at my laptop.

Chaining would likely work

woeful lantern
#

Can I for loop it?

hollow sail
#

You could for loop it.

But that's why I suggest passing the list to id directly. Have you tried this first suggestion?

woeful lantern
#

Let me try it. I didn't know that you could pass the list directly.

woeful lantern
#

No.. you can't do that.

short moth
#
class CustomBulkOfReportsQuerySet(models.QuerySet):
    def between_by_report(self, first_report, last_report):
        reports = Report.objects.filter(
            jurnal__d_j_order__gte=first_report.jurnal.d_j_order,
            jurnal__d_j_order__lte=last_report.jurnal.d_j_order,
        )

        d_j_order_matches = []
        for report in reports:
            d_j_order_matches.append(report.jurnal.d_j_order)

        return self.filter(reports__jurnal__d_j_order=d_j_order_matches).distinct()
#

Output:

#

Field 'd_j_order' expected a number but got [2].

short moth
woeful lantern
timid hull
#
from django.db.models import Count

target_ids = {1, 2, 3}

result = (
    BulkOfReports.objects.annotate(
        reports_count=Count("reports")
    )
    .filter(
        reports_count=len(target_ids),
        reports__id__in=target_ids
    )
    .distinct()
)
#

Can you please try this?

cunning grail
#

Tbh, infind this question a bit odd - how would you find the list of reports in a bulk without looking up the bulk first? And why do you need to look up bulkreports based on the exact set of reports in them ๐Ÿค”

woeful lantern
woeful lantern
#
urlpatterns: list[URLPattern] = [
path(
        "<slug:unit_handle>/<slug:first_report_id>...<slug:last_id>/bulk_of_reports_view_<slug:version>/",
        views.bulk_of_reports_view___simple,
        name="bulk_of_reports_view___simple",
    ),
]
woeful lantern
woeful lantern
cunning grail
woeful lantern
#

Just right now.

woeful lantern
cunning grail
#

Yeah, but if you already know what reports should be in a bundle, why do you even need the objects? ๐Ÿ˜…

woeful lantern
#

It's actually not just id and M2MField

cunning grail
#

So you know the mapping, but not the uuid? ๐Ÿ˜…

woeful lantern
cunning grail
#

So why just not do the query by uuid?

woeful lantern
#

I want the URL to be shareable to other user, therefore I think it makes sense to have the link not throw a bunch of weird numbers and letters, y'know.

cunning grail
#

Imho it makes more sense to share a link with a uniqe ID rather than a link that is a the mercy of the input order ine the database ๐Ÿค”