close
Skip to main content
charettes u/charettes avatar

charettes

u/charettes

Feed options
Hot
New
Top
View
Card
Compact

It's not mentioned in the post nor here so I figured I'd point a feature often overlooked.

It's possible to target managers used for reverse relationships by calling them and referencing the manager by name.

As documented overriding the default manager's get_queryset method comes with gotchas but if you define an active_objects manager instead you could target it by doing

store.products(manager="active_objects")

If you find this topic interesting you might be interested in upcoming support to define filtered managers inline

class Product(models.Models):
    is_active = models.BooleanField()

    objects = models.Manager()
    active_objects = QuerySet.filter(is_active=True).as_manager()

Merci Fred!


Vengeance pally must deal with melee proximity and requirements to get life after hit / kill to be sustainable though while buriza multi-shot can achieve high dps at distance with physical life leach.


Very hard to tell what might be wrong without you providing a sample project to test it out. All I can you is that I tested the above against Django 5.2 and SQLite and it's working flawlessly

https://cdn.zappy.app/10ac47dd72e2be7198b0fa2d7bb9bd52.png

Now obviously if you use methods that bypass model validation you'll get an IntegrityError instead of a ValidationError.


Apparently the change to support the __transform syntax didn't make the cut in Django 5.2 so you'll want to stick to TruncDate. I'll adjust my reply accordingly.

Here's the ticket for reference.


This is close to the right answer but it lacks the per-machine part of the request

It's worth pointing out that it will use the UTC date, which OP didn't specify, but is an important part of the problem.

FWIW __date transform syntax can also be used to avoid the import (this will only work in Django 6.0+) and you can specify the error message that should displayed on violation by using violation_error_message. All of that can be combined under

from django.db import models

class MachineReading(models.Model):
    ...
    created = models.DateTimeField()

    class Meta:
        constraints = [
            models.UniqueConstraint(
                "machine",
                TruncDate("created"),
                name='uc_date_created',
                violation_error_message=(
                    "Only one reading per machine per day is allowed"
                ),
            ),
        ]

I think they were referring to django-rosetta that already does something very similar to your project.


Are there any particular change that worries you more than others? Most of them should be pretty rarely encountered by a typical user or third-party application.