django-managers

django soft delete doesn't cascade delete

你说的曾经没有我的故事 提交于 2019-12-10 17:37:08
问题 I'm using a soft delete in my django admin, done like this. The issue is that when I delete a foreign key item, that it doesn't seem to trigger the deletes for all the items it's linked to. Or maybe it does but it's not running the custom def delete I have on my model. -If I delete a person then they are soft-deleted, but the related account is left untouched. -If I remove the soft deletes, then when I delete a Person, the Accounts are deleted too which is correct. So ideally when I delete a

Django - User creation with custom user model results in internal error

南楼画角 提交于 2019-12-08 03:38:01
问题 Ok, I know this is a silly question but I am blocked and I can't figure out what to do. I have searched on google and stackoverflow but did not found any answer : I tried this : Adding custom fields to users in django Django - Create user profile on user creation https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users My model is the following : class UserProfile(models.Model): user = models.OneToOneField(User) quota = models.IntegerField(null = True) def

Override update method for a queryset - Django

元气小坏坏 提交于 2019-12-07 14:44:05
问题 How can we change the default process of update method for a queryset in django as it does not call save method for each object. And since i have overridden the save method, i need it to be called each time the object is changed. I looked for django doc but this is just for get_query_set , Is there is something similar for update method also. Thanks 回答1: This worked for me class MyQuerySet(models.query.QuerySet): def update(self, *args, **kwargs): # here queryset update method overridden pass

Django pagination (get page no. corresponding to the object)

谁说胖子不能爱 提交于 2019-12-07 08:18:55
问题 I have a paginate I am trying to get the index page from an object page (sort of pagination in reverse) The get_paginated_posts returns a paginator for the model Post : class PostManager(models.Manager): def get_paginated_posts(self, request=None): if request and request.user.has_perm('blog.change_post'): posts = super(PostManager, self).filter(is_update=False) else: posts = super(PostManager, self).filter(publish=True, is_update=False) return Paginator(posts, POSTS_PER_PAGE) . . This is my

Django pagination (get page no. corresponding to the object)

◇◆丶佛笑我妖孽 提交于 2019-12-05 13:31:22
I have a paginate I am trying to get the index page from an object page (sort of pagination in reverse) The get_paginated_posts returns a paginator for the model Post : class PostManager(models.Manager): def get_paginated_posts(self, request=None): if request and request.user.has_perm('blog.change_post'): posts = super(PostManager, self).filter(is_update=False) else: posts = super(PostManager, self).filter(publish=True, is_update=False) return Paginator(posts, POSTS_PER_PAGE) . . This is my model class Post(models.Model): . . . def get_page(self, request=None): paginator = Post.objects.get

Django ORM. Joining subquery

怎甘沉沦 提交于 2019-12-04 15:05:29
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 site. If I use: Site.objects.filter(stats__date=my_date) I will not get sites which have no records for

Django Model vs. Manager

北战南征 提交于 2019-12-04 04:15:47
Not really sure what the difference is. Seems like all the Manager does is have a bunch of functions related to the Model. But these functions could also be placed in the Model too.... Django documentation describes the Manager as follows, A Manager is the interface through which database query operations are provided to Django models. So is there anything else fundamentally different about the Manager than this simple abstraction? Or a better question: what methods should be defined in the Model vs. the Manager? Is there an ACTUAL difference or just stylistic one? In Django, a models' manager

How does use_for_related_fields work in Django?

懵懂的女人 提交于 2019-12-03 08:51:19
问题 I'm unable to grasp that from the docs. It's totally unclear to me, more specifically: Is it a global setting? So if I specify this attribute it on one of the model managers, will it be used globally by all the model classes? If it's not a global setting then which relationships exactly will be affected? Is it possible to have one model manager for one relationship and another one for another relationship with the same model? Most of all I would appreciate any good minimal example usages as

Where should django manager code live?

ε祈祈猫儿з 提交于 2019-12-03 07:46:09
问题 This is a pretty simple django patterns question. My manager code usually lives in models.py, but what happens when models.py is really huge? Is there any other alternative pattern to letting your manager code live in models.py for maintainability and to avoid circular imports? A question may be asked as to why models.py is so huge, but let's just assume it's size and breadth of utility is justified. 回答1: I prefer to keep my models in models.py and managers in managers.py (forms in forms.py)

How does use_for_related_fields work in Django?

眉间皱痕 提交于 2019-12-02 22:44:01
I'm unable to grasp that from the docs. It's totally unclear to me, more specifically: Is it a global setting? So if I specify this attribute it on one of the model managers, will it be used globally by all the model classes? If it's not a global setting then which relationships exactly will be affected? Is it possible to have one model manager for one relationship and another one for another relationship with the same model? Most of all I would appreciate any good minimal example usages as the documentation lacks those afaik. Thanks. Is it a global setting? So if I specify this attribute it