django-managers

Django - Runtime database switching

人盡茶涼 提交于 2019-12-18 21:19:44
问题 In my work we want to run a server with multiple databases. The databases switching should occur when you acces a url like http://myapp.webpage.com or http://other.webpage.com . We want to run only one server instance and at the moment of the HTTP request switch the database and return the corresponding response. We've been looking for a mantainable and 'Django-friendly' solution. In our investigation we have found possible ways to do this, but we have not enough information about. Option 1:

Manager isn't accessible via model instances

大兔子大兔子 提交于 2019-12-17 17:43:29
问题 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)

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

柔情痞子 提交于 2019-12-17 07:05:44
问题 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

Django: exceptions and returns, what is the correct way to handle multiple potential situations?

霸气de小男生 提交于 2019-12-13 19:17:36
问题 I have a function in a Manager which activates a user account via a key. When a key is provided, several checks need to be performed: does the key exist? has the key expired? and then if not, the manager activates the account. def activate(key): try: profile = self.get(key=key) except self.model.DoesNotExist: return None if not profile.key_expired(): ## Activate user return user return None The issue is of course, that this returns False for both 'the key doesn't exist', and 'the key given is

Django custom creation manager logic for temporal database

前提是你 提交于 2019-12-13 03:22:48
问题 I am trying to develop a Django application that has built-in logic around temporal states for objects. The desire is to be able to have a singular object representing a resource, while having attributes of that resource be able to change over time. For example, a desired use case is to query the owner of a resource at any given time (last year, yesterday, tomorrow, next year, ...). Here is what I am working with... class Resource(models.Model): id = models.AutoField(primary_key=True) class

What's the difference between Custom model manager methods and QuerySet methods?

风流意气都作罢 提交于 2019-12-12 12:35:27
问题 I am trying to get my head around writing Custom managers. I found the online documentation a little sparse. Toying around myself with code, I discovered the following patterns: Given the following model... class QuestionQuerySet(models.QuerySet): def QS_first (self): return self.first() class QuestionManager(models.Manager): def get_queryset(self): return QuestionQuerySet(self.model, using=self._db) def MN_first(self): return self.get_queryset().first() class Question(models.Model): front =

django custom manager with filter parameter

不羁的心 提交于 2019-12-12 09:37:46
问题 I would like to add a custom manager which can be called from a template, but does not affect the entire model (e.g. admin views) and which listens to a parameter set in the request (user_profile). The following is what I have so far: models.py: class CurrentQuerySet(models.query.QuerySet): def current(self): return self.filter(id=1) ## this simplified filter test works.. class CurrentManager(models.Manager): use_for_related_fields = True def get_query_set(self): return CurrentQuerySet(self

Django Model vs. Manager

允我心安 提交于 2019-12-12 08:38:12
问题 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

Django, which function belongs to QuerySet and Manager?

╄→гoц情女王★ 提交于 2019-12-12 03:24:46
问题 I used to think QuerySet method return QuerySet instances, but it apparently not. For instance, count() is a queryset method not manager's How do I decide which functions go to custom QuerySet and which go to custom Manager class? 回答1: It makes sense to be able to access some functions like count() on the manager and the queryset. This allows you to do: Blog.objects.count() # total number of blogs Blog.objects.filter(status='PUBLISHED').count() # Number of published blogs Django has a method

Single custom Manager for Multiple models in Django

谁说我不能喝 提交于 2019-12-11 01:19:19
问题 I have several models connected to each other with ForeignKeys relationships. The main one in this sort of hierarchy contains a owner field. I would like to create a single custom manager for all these models that changes the returned queryset depending on the models that is calling it. I know that manager can access self.model to get the model that it is attached to. Class Main(models.Model) owner=models.ForeignKey (User) owned = OwnedManager() Class Second(models.Model) main=models