django-managers

Django queryset with Model method containing another queryset

这一生的挚爱 提交于 2021-02-08 07:17:16
问题 Suppose I have a model, MyModel , with a property method that uses another model's queryset. class OtherModel(models.Model) ... class MyModel(models.Model): simple_attr = models.CharField('Yada yada') @property def complex_attr(self): list_other_model = OtherModel.objects.all() ... # Complex algorithm using queryset from 'OtherModel' and simple_attr return result This causes my get_queryset() method on MyModel to query the database to generate the list_other_model variable every time for

Recursive relationship in Django: get all the interrelated records from each record

戏子无情 提交于 2021-01-29 15:13:44
问题 I have this model for a tune: class MsTune(models.Model): name = models.CharField(max_length=255) ms = models.ForeignKey(Manuscript, on_delete=models.CASCADE, related_name="mstunes") concordances = models.ManyToManyField("self", blank=True) I have many manuscripts (ms), and the same tune might appear in more than one manuscript. What I want to do, with that concordances field, is link all the similar tunes. This works, in part. The Django administration back-end allows me to select many tunes

Creating a django manager with a parameter

泪湿孤枕 提交于 2020-12-30 08:49:03
问题 I have the following situation I have a manager class that filters a queryset according to a field. The problem is that the field name is different according to the class but the value to which it filters comes from the same place (so i thought i don't need several managers). This is what i did so far. class MyManager(models.Manager): def __init__(self, field_name): super(MyManager, self).__init__() self.field_name = field_name def get_queryset(self): # getting some value kwargs = { self

Creating a django manager with a parameter

徘徊边缘 提交于 2020-12-30 08:42:27
问题 I have the following situation I have a manager class that filters a queryset according to a field. The problem is that the field name is different according to the class but the value to which it filters comes from the same place (so i thought i don't need several managers). This is what i did so far. class MyManager(models.Manager): def __init__(self, field_name): super(MyManager, self).__init__() self.field_name = field_name def get_queryset(self): # getting some value kwargs = { self

Creating a django manager with a parameter

淺唱寂寞╮ 提交于 2020-12-30 08:42:04
问题 I have the following situation I have a manager class that filters a queryset according to a field. The problem is that the field name is different according to the class but the value to which it filters comes from the same place (so i thought i don't need several managers). This is what i did so far. class MyManager(models.Manager): def __init__(self, field_name): super(MyManager, self).__init__() self.field_name = field_name def get_queryset(self): # getting some value kwargs = { self

Manager isn't accessible via “model” instances django

六月ゝ 毕业季﹏ 提交于 2019-12-25 02:16:17
问题 I am suffering an error with django and their custom Managers. I have this custom Manager: class CallManager(models.Manager): def get_queryset(self): return super(CallManager, self).get_queryset().filter(is_active=True) class Call(models.Model): ... # Data is_active = models.BooleanField(default=True) # Managers objects = models.Manager() # Default active = CallManager() # Active calls Ok, So now I am trying to retrive data from views.py (call/views.py) # Call list def call_list(request):

Django ORM. Joining subquery

左心房为你撑大大i 提交于 2019-12-21 21:28:12
问题 I have a table which contains list of some web sites and a table with statistics of them. class Site(models.Model): domain_name = models.CharField( max_length=256, unique=True, ) class Stats(models.Model): date = models.DateField() site = models.ForeignKey('Site') google_pr = models.PositiveIntegerField() class Meta: unique_together = ('site', 'date') I want to see all sites and statistics for a concrete date. If a stats record for the date doesn't exist, then the selection must contain only

How to add a Manager from Field

狂风中的少年 提交于 2019-12-21 21:10:14
问题 What i want to do is when some model use my field, it will automaticaly add custom manager to that model. As far as i know, contibute_to_class provide such functionality class MyCustomField(CharField): def contribute_to_class(self, cls, name): super(MyCustomField, self).contribute_to_class(cls, name) setattr(cls, 'custom_manager', CustomManager()) The problem is that in my custom manager i use self.model._default_manager to do queries on default manager but when i try to do it, django says

Django custom model managers

断了今生、忘了曾经 提交于 2019-12-20 10:00:14
问题 I'm confused about the correct way to use Django custom model managers - based on the docs you can create a series of managers for one model as a way of filtering. But why not create one manager class with a series of functions for filtering? Is one method better than the other? and why? For example: class MaleManager(models.Manager): def get_query_set(self): return super(MaleManager, self).get_query_set().filter(sex='M') class FemaleManager(models.Manager): def get_query_set(self): return

Django Manager Chaining

独自空忆成欢 提交于 2019-12-20 08:21:29
问题 I was wondering if it was possible (and, if so, how) to chain together multiple managers to produce a query set that is affected by both of the individual managers. I'll explain the specific example that I'm working on: I have multiple abstract model classes that I use to provide small, specific functionality to other models. Two of these models are a DeleteMixin and a GlobalMixin. The DeleteMixin is defined as such: class DeleteMixin(models.Model): deleted = models.BooleanField(default=False