django-managers

How to use custom manager with related objects?

China☆狼群 提交于 2019-11-26 19:56:42
问题 I have a custom manager. I want to use it for related objects. I found use_for_related_fields in docs. But it does not work the way I used it: class RandomQueryset(models.query.QuerySet): def randomize(self): count = self.count() random_index = random.randint(0, count - 1) return self.all()[random_index] class RandomManager(models.Manager): use_for_related_fields = True def get_query_set(self): return RandomQueryset(self.model, using=self._db) def randomize(self): return self.get_query_set()

Custom QuerySet and Manager without breaking DRY?

馋奶兔 提交于 2019-11-26 17:15:46
I'm trying to find a way to implement both a custom QuerySet and a custom Manager without breaking DRY. This is what I have so far: class MyInquiryManager(models.Manager): def for_user(self, user): return self.get_query_set().filter( Q(assigned_to_user=user) | Q(assigned_to_group__in=user.groups.all()) ) class Inquiry(models.Model): ts = models.DateTimeField(auto_now_add=True) status = models.ForeignKey(InquiryStatus) assigned_to_user = models.ForeignKey(User, blank=True, null=True) assigned_to_group = models.ForeignKey(Group, blank=True, null=True) objects = MyInquiryManager() This works fine

How to Unit test with different settings in Django?

别说谁变了你拦得住时间么 提交于 2019-11-26 12:22:04
问题 Is there any simple mechanism for overriding Django settings for a unit test? I have a manager on one of my models that returns a specific number of the latest objects. The number of objects it returns is defined by a NUM_LATEST setting. This has the potential to make my tests fail if someone were to change the setting. How can I override the settings on setUp() and subsequently restore them on tearDown() ? If that isn\'t possible, is there some way I can monkey patch the method or mock the

Custom QuerySet and Manager without breaking DRY?

旧巷老猫 提交于 2019-11-26 05:19:52
问题 I\'m trying to find a way to implement both a custom QuerySet and a custom Manager without breaking DRY. This is what I have so far: class MyInquiryManager(models.Manager): def for_user(self, user): return self.get_query_set().filter( Q(assigned_to_user=user) | Q(assigned_to_group__in=user.groups.all()) ) class Inquiry(models.Model): ts = models.DateTimeField(auto_now_add=True) status = models.ForeignKey(InquiryStatus) assigned_to_user = models.ForeignKey(User, blank=True, null=True) assigned