django-orm

How to work around lack of support for foreign keys across databases in Django

眉间皱痕 提交于 2019-12-28 07:36:07
问题 I know Django does not support foreign keys across multiple databases (originally Django 1.3 docs) But I'm looking for a workaround. What doesn't work I have two models each on a separate database. routers.py: class NewsRouter(object): def db_for_read(self, model, **hints): if model._meta.app_label == 'news_app': return 'news_db' return None def db_for_write(self, model, **hints): if model._meta.app_label == 'news_app': return 'news_db' return None def allow_relation(self, obj1, obj2, **hints

How to edit m2m in Django via ModelForm

可紊 提交于 2019-12-25 12:01:17
问题 I have 2 models: class Case(models.Model): Priority = ( ('Blocker', 'Blocker'), ('High', 'High'), ('Medium', 'Medium'), ('Low', 'Low'), ) id = models.AutoField(primary_key=True) name = models.CharField(max_length=200) image = models.URLField(blank=True) requirements = models.URLField(blank=True) priority = models.CharField(max_length=10, choices=Priority) description = models.CharField(max_length=200) step = models.TextField() modified = models.DateTimeField(editable=False) user = models

Getting the many-to-many fields of a many-to-many object

穿精又带淫゛_ 提交于 2019-12-25 09:29:07
问题 If I have the user-group relation as a ManyToMany relationship, and a group-team relation as a ManyToMany relationship, how would I find the user-team relation as a query object? For example if I had a models.py like this: class Group(models.Model): name = CharField(max_length=100) class User(models.Model): username = models.CharField(max_length=100) groups = models.ManyToManyField(Group, blank=True) class Team(models.Model): teamname = models.CharField(max_length=100) groups = models

Django: Filtering Annotated Results

隐身守侯 提交于 2019-12-25 08:36:17
问题 I have two models: class Status(models.Model): title = models.CharField(max_length=32) class Task(models.Model): user = models.ForeignKey(User) status = models.ForeignKey(Status, default=1) title = models.CharField(max_length=128) I want to create a nav list that contains all of the statuses I have in my status model, example: Today, Tomorrow, Waiting, Scheduled, Trash Simple enough. I then want to display the number of tasks assigned to each status, thanks to SO, also simple: Status.objects

Django - get all objects that don't belong to M2M

女生的网名这么多〃 提交于 2019-12-25 07:50:21
问题 I have a model with field: class Product(models.Model): subproducts = models.ManyToManyField("self", blank=True) I need to overwrite admin's field queryset, to display only that objects that don't belong to any m2m relation. I have no idea how to get them. So if I have: product1, product2, product3, product4. product1 contains in subproducts: product2 I need a query that will get, in that situation, product3 and product4 Any idea how to get that? 回答1: I think that did the trick: Product

Filter with arbitrary number or Q objects combined by OR logical operator

微笑、不失礼 提交于 2019-12-25 07:24:02
问题 Django 1.10.1 Search form. The user inserts words separated by spaces. Necessary to find objects with ANY of these words in the title field. I'm planning to use something like this: Article.objects.filter( Q(title__icontains="table") | Q(title__icontains="helm") ) I can make Q objects easily: q = Q(title__icontains="table"). But the obstacle is how to pass arguments to the filter method. https://docs.djangoproject.com/en/1.10/topics/db/queries/ The syntax is filter(**kwargs) . With a loop I

Executing Anti Join in Django ORM

為{幸葍}努か 提交于 2019-12-25 07:09:51
问题 I have two models: class Note(model): <attribs> class Permalink(model): note = foreign key to Note I want to execute a query: get all notes which don't have a permalink. In SQL, I would do it as something like: SELECT * FROM Note WHERE id NOT IN (SELECT note FROM Permalink); Wondering how to do this in ORM. Edit: I don't want to get all the permalinks out into my application. Would instead prefer it to run as a query inside the DB. 回答1: you can use: Note.objects.exclude(id__in=Permalink

Django - Create the equivalent of a “cross-join” query with the Django ORM

谁说胖子不能爱 提交于 2019-12-25 04:16:13
问题 I have two Django models with are related to each other through a many-to-many relationship. I need to list the cross product of both tables. Let say for simplicity's sake that the two models are Pizza and Topping. I would like the query to return something like this: pizza_name topping --------------------- all dressed cheese all dressed mushrooms all dressed onions all dressed peperoni all dressed pepper reddit cheese reddit peperoni reddit bacon reddit baconbits The amount of data will be

Django JSONField dynamic contains and key lookups

只谈情不闲聊 提交于 2019-12-25 02:44:34
问题 I need to filter objects by key-value pair contained in JSONField with list of dicts where key and value are model fields. This question is related to Django JSONField list of dicts "contains" filter inside Subquery This one works perfectly: EntityObject.objects.filter( data__list_of_dicts__contains=[{'static_key': 'static_value'}] ) But with dynamic values: EntityObject.objects.filter( data__list_of_dicts__contains=[{F('field1'): F('field2')}] ) It causes an error: TypeError: keys must be a

Django ORM: how count in annotation differ from count on query set?

血红的双手。 提交于 2019-12-25 00:02:31
问题 qs = ... qs = qs.annotate(v=Count('a', filter=Q(a__lt=5))) a = qs.first().v b = qs.filter(Q(a__lt=5)).count() assert a == b # error Is there any reason why these methods could produce different results? 回答1: From the documentation about Count(expression, **kwargs) : Returns the number of objects that are related through the provided expression So Count is specifically meant to count related objects (through FK or M2M relationships), and doesn't make much sense on any other column of the row