django-orm

Django: implementing JOIN using Django ORM?

不问归期 提交于 2019-11-27 08:22:06
I have a Q&A type of site built in Django with the following models: class Question(models.Model): title = models.CharField(max_length=70) details = models.TextField() class Answer(models.Model): question_id = IntegerField() details = models.TextField() I need to display a specific question together with its answers. Normally I'd need 2 queries to do that: Question.objects.get(id=1) Answer.objects.get(question_id=1)[:10] I'm hoping to retrieve everything using one query. In MySQL it'd be: SELECT * FROM Question JOIN Answer ON Question.id=Answer.question_id WHERE Question.id=1 LIMIT 10 Is there

Why is iterating through a large Django QuerySet consuming massive amounts of memory?

大憨熊 提交于 2019-11-27 06:09:34
The table in question contains roughly ten million rows. for event in Event.objects.all(): print event This causes memory usage to increase steadily to 4 GB or so, at which point the rows print rapidly. The lengthy delay before the first row printed surprised me – I expected it to print almost instantly. I also tried Event.objects.iterator() which behaved the same way. I don't understand what Django is loading into memory or why it is doing this. I expected Django to iterate through the results at the database level, which'd mean the results would be printed at roughly a constant rate (rather

Filtering on many-to-many relations that fulfill a set of criteria

和自甴很熟 提交于 2019-11-27 04:52:33
问题 With the following models: class OrderOperation(models.Model): ordered_articles = models.ManyToManyField(Article, through='orders.OrderedArticle') class OrderedArticle(models.Model): order_operation = models.ForeignKey(OrderOperation) article = models.ForeignKey(Article) articles = ... # some queryset containing multiple articles If I want to find order operations containing at least one article, this works as expected: OrderOperation.objects.filter(ordered_articles__in=articles) However, if

What is the default order of a list returned from a Django filter call?

↘锁芯ラ 提交于 2019-11-27 04:32:46
问题 Short Question What is the default order of a list returned from a Django filter call when connected to a PostgreSQL database? Background By my own admission, I had made a poor assumption at the application layer in that the order in which a list is returned will be constant, that is without using 'order_by'. The list of items I was querying is not in alphabetic order or any other deliberate order. It was thought to remain in the same order as which they were added to the database. This

How to map PostgreSQL array field in Django ORM

一个人想着一个人 提交于 2019-11-27 03:52:49
问题 I have an array field in my PostrgreSQL database of type text. Is there a way to map this into a Django model ? 回答1: You might want to look into django-dbarray on github. It adds support for postgresql array fields. I haven't used it before, but it looks like you just need to do: from django.db import model import dbarray class ProfilingTestRun(models.Model): function = models.CharField(max_length=64) runtimes = dbarray.FloatArrayField() 回答2: One of the other nice options is http://django-orm

Django self-referential foreign key

99封情书 提交于 2019-11-27 03:35:06
I'm kind of new to webapps and database stuff in general so this might be a dumb question. I want to make a model ("CategoryModel") with a field that points to the primary id of another instance of the model (its parent). class CategoryModel(models.Model): parentId = models.ForeignKey(CategoryModel) How do I do this? Thanks! You can pass in the name of a model as a string to ForeignKey and it will do the right thing. So: parentId = models.ForeignKey("CategoryModel") Or you can use the string "self" parentId = models.ForeignKey("self") Brandon You can use the string 'self' to indicate a self

How to rename items in values() in Django?

给你一囗甜甜゛ 提交于 2019-11-27 03:02:21
I want to do pretty much the same like in this ticket at djangoproject.com , but with some additonal formatting. From this query >>> MyModel.objects.values('cryptic_value_name') [{'cryptic_value_name': 1}, {'cryptic_value_name': 2}] I want to get something like that: >>> MyModel.objects.values(renamed_value='cryptic_value_name') [{'renamed_value': 1}, {'renamed_value': 2}] Is there another, more builtin way or do I have to do this manually? Daniel Roseman It's a bit hacky, but you could use the extra method: MyModel.objects.extra( select={ 'renamed_value': 'cryptic_value_name' } ).values(

Django: select_related and GenericRelation

半世苍凉 提交于 2019-11-27 01:41:57
问题 Does select_related work for GenericRelation relations, or is there a reasonable alternative? At the moment Django's doing individual sql calls for each item in my queryset, and I'd like to avoid that using something like select_related. class Claim(models.Model): proof = generic.GenericRelation(Proof) class Proof(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') I'm

Accessing Many to Many “through” relation fields in Formsets

被刻印的时光 ゝ 提交于 2019-11-27 01:29:52
问题 My Models: class End_User(models.Model): location = models.ForeignKey(Location) first_name = models.CharField(max_length=70, blank=True, null=True) email_address = models.CharField(max_length=70, blank=True, null=True) class Phone_Client(models.Model): end_user = models.ManyToManyField(End_User) ... extensions = models.CharField(max_length=20) class Line(models.Model): phone_client = models.ManyToManyField(Phone_Client, through='Phone_Line' ) .... voicemail = models.BooleanField(default=False

prefetch_related for multiple Levels

只愿长相守 提交于 2019-11-27 00:54:04
问题 If my Models look like: class Publisher(models.Model): pass class Book(models.Model): publisher = models.ForeignKey(Publisher) class Page(models.Model): book = models.ForeignKey(Book) and I would like to get the queryset for Publisher I do Publisher.object.all() . If then want to make sure to prefetch I can do: Publisher.objects.all().prefetch_related('book_set')` My questions are: Is there a way to do this prefetching using select_related or must I use prefetch_related ? Is there a way to