django-managers

Django ORM - objects.filter() vs. objects.all().filter() - which one is preferred?

旧城冷巷雨未停 提交于 2019-11-30 07:51:23
Very often I see constructs like MyModel.objects.all().filter(...) which will return a QuerySet of the default Mananger. At first all() seems to be quite redundant, because MyMode.objects.filter(...) delivers the same result. However, this seems to be safe for the default Manager only, because of the following two statements in the Django documentation: Excerpt from the Chapter "Adding extra manager methods" A custom Manager method can return anything you want. It doesn’t have to return a QuerySet. Definition of the all() manager method: all() Returns a copy of the current QuerySet (or

User manager methods create() and create_user()

 ̄綄美尐妖づ 提交于 2019-11-30 03:34:49
问题 I have encountered with some suspicious behavior of create() method of User object manager. Looks like password field isn't required for creating User object if you use this method. In result you get User with blank password . In case when you use create_user method and don't specify password it creates User with unusable password (through to set_unusable_password() ). I am not sure why create() method doesn't raise exception when you try to create user without password - in documentation it

Django ORM - objects.filter() vs. objects.all().filter() - which one is preferred?

ⅰ亾dé卋堺 提交于 2019-11-29 10:36:53
问题 Very often I see constructs like MyModel.objects.all().filter(...) which will return a QuerySet of the default Mananger. At first all() seems to be quite redundant, because MyMode.objects.filter(...) delivers the same result. However, this seems to be safe for the default Manager only, because of the following two statements in the Django documentation: Excerpt from the Chapter "Adding extra manager methods" A custom Manager method can return anything you want. It doesn’t have to return a

AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django?

ⅰ亾dé卋堺 提交于 2019-11-28 21:26:26
I am using Django '1.5c1' . I have this line in my settings.py: AUTH_USER_MODEL = 'fileupload.galaxyuser' Here's my Galaxyuser model: class GalaxyUser(models.Model): id = models.IntegerField(primary_key=True) create_time = models.DateTimeField(null=True, blank=True) update_time = models.DateTimeField(null=True, blank=True) email = models.CharField(max_length=765) password = models.CharField(max_length=120) external = models.IntegerField(null=True, blank=True) deleted = models.IntegerField(null=True, blank=True) purged = models.IntegerField(null=True, blank=True) username = models.CharField(max

select_related with reverse foreign keys

时光怂恿深爱的人放手 提交于 2019-11-28 06:17:06
I have two Models in Django. The first has the hierarchy of what job functions (positions) report to which other positions, and the second is people and what job function they hold. class PositionHierarchy(model.Model): pcn = models.CharField(max_length=50) title = models.CharField(max_length=100) level = models.CharField(max_length=25) report_to = models.ForeignKey('PositionHierachy', null=True) class Person(model.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) ... position = models.ForeignKey(PositionHierarchy) When I have a Person record and

Manager isn't accessible via model instances

让人想犯罪 __ 提交于 2019-11-28 04:49:01
i'm trying to get model objects instance in another one. And i raise this error : Manager isn't accessible via topic instance Here's my model : class forum(models.Model): # Some attributs class topic(models.Model): # Some attributs class post(models.Model): # Some attributs def delete(self): forum = self.topic.forum super(post, self).delete() forum.topic_count = topic.objects.filter(forum = forum).count() Here's my view : def test(request, post_id): post = topic.objects.get(id = int(topic_id)) post.delete() And i get : post.delete() forum.topic_count = topic.objects.filter(forum = forum).count

How to use custom manager with related objects?

我的梦境 提交于 2019-11-27 19:37:25
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().randomize() I used it for one model: >>> post = PostPages.default_manager.filter(image_gallery__isnull

AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django?

≯℡__Kan透↙ 提交于 2019-11-27 13:58:47
问题 I am using Django '1.5c1' . I have this line in my settings.py: AUTH_USER_MODEL = 'fileupload.galaxyuser' Here's my Galaxyuser model: class GalaxyUser(models.Model): id = models.IntegerField(primary_key=True) create_time = models.DateTimeField(null=True, blank=True) update_time = models.DateTimeField(null=True, blank=True) email = models.CharField(max_length=765) password = models.CharField(max_length=120) external = models.IntegerField(null=True, blank=True) deleted = models.IntegerField

Django custom managers - how do I return only objects created by the logged-in user?

浪子不回头ぞ 提交于 2019-11-27 03:01:34
I want to overwrite the custom objects model manager to only return objects a specific user created. Admin users should still return all objects using the objects model manager. Now I have found an approach that could work. They propose to create your own middleware looking like this: #### myproject/middleware/threadlocals.py try: from threading import local except ImportError: # Python 2.3 compatibility from django.utils._threading_local import local _thread_locals = local() def get_current_user(): return getattr(_thread_locals, 'user', None) class ThreadLocals(object): """Middleware that

select_related with reverse foreign keys

醉酒当歌 提交于 2019-11-27 01:15:37
问题 I have two Models in Django. The first has the hierarchy of what job functions (positions) report to which other positions, and the second is people and what job function they hold. class PositionHierarchy(model.Model): pcn = models.CharField(max_length=50) title = models.CharField(max_length=100) level = models.CharField(max_length=25) report_to = models.ForeignKey('PositionHierachy', null=True) class Person(model.Model): first_name = models.CharField(max_length=50) last_name = models