django-select-related

Specifying order_by on Related Field in Django

我的未来我决定 提交于 2020-01-05 19:17:59
问题 In Django, on a given model it looks like the RelatedManager may only be set statically (see: docs) but is there any way to set the order_by on the related manager for a given Queryset? I tried using prefetch_related and Prefetch , but that does not seem to work: qs = qs.prefetch_related( Prefetch('item_set', queryset=Item.objects.order_by('capture_dt').all())) res = qs.first().item_set.all().ordered In this case res is False . The problem comes when I try to access the related manager:

Selecting specific fields using select_related in Django

落花浮王杯 提交于 2019-12-21 06:50:57
问题 I have two models Article and Blog related using a foreign key. I want to select only blog name while extracting the article. articles = Articles.objects.all().select_related('blog__name') The query generated shows that it selected all the fields from the Blog model. I tried using only() and defer() with select_related but both didn't work out. articles = Articles.objects.all().select_related('blog__name').only('blog__name', 'title', 'create_time') The above query resulted in error: Invalid

Is there a way to check whether a related object is already fetched?

 ̄綄美尐妖づ 提交于 2019-12-10 19:04:34
问题 I would like to be able to check if a related object has already been fetched by using either select_related or prefetch_related , so that I can serialize the data accordingly. Here is an example: class Address(models.Model): street = models.CharField(max_length=100) zip = models.CharField(max_length=10) class Person(models.Model): name = models.CharField(max_length=20) address = models.ForeignKey(Address) def serialize_address(address): return { "id": address.id, "street": address.street,

django - dynamic select fields in forms

好久不见. 提交于 2019-12-10 18:30:42
问题 I have a model called picks that allows users to select an nfl team (from a static list). Whenever they choose a team, they can no longer choose that team again, so there selection choices are reduced by any teams they have chosen. I have a function that I call in my view that figures out what teams they have not selected yet, but I cannot figure out how to pass this information to the form. I've looked at many form examples online but have not really found one that shows how to accomplish

In Django, Can I `defer()` fields in an object that's being queried by `select_related()`

让人想犯罪 __ 提交于 2019-12-09 10:59:24
问题 In my Django app I want to use select_related() on a QuerySet to "follow" a ForeignKey field, but I only need to access a few of the fields on the "followed" model instance. Can I use the defer() method somehow with my "followed" field. e.g., if I have... class BarModel(models.Model): ... blah = models.TextField() class FooModel(models.Model): bar = models.ForeignKey(BarModel) ... ...and I'm doing FooModel.objects.all().select_related('bar') how can I defer() the field blah . Thanks. 回答1:

Selecting specific fields using select_related in Django

烂漫一生 提交于 2019-12-04 00:02:55
I have two models Article and Blog related using a foreign key. I want to select only blog name while extracting the article. articles = Articles.objects.all().select_related('blog__name') The query generated shows that it selected all the fields from the Blog model. I tried using only() and defer() with select_related but both didn't work out. articles = Articles.objects.all().select_related('blog__name').only('blog__name', 'title', 'create_time') The above query resulted in error: Invalid field name(s) given in select_related: Choices are: blog How do i generate a query so that only article

Django admin performance issue

£可爱£侵袭症+ 提交于 2019-12-02 05:29:41
问题 I'm getting thousands of these queries when I try to open up a model in the Django admin interface and it's leading to a serious performance issue. [sql] SELECT ... FROM `auth_user` WHERE `auth_user`.`id` = 9535 [sql] (21ms) Found 1 matching rows [sql] SELECT ... FROM `auth_user` WHERE `auth_user`.`id` = 9536 [sql] (20ms) Found 1 matching rows Any ideas why Django admin isn't using select_related()? Here are (I think) the relevant parts of the model (I'm looking at an instance of the Student

Get related column on .annotate() data Django

荒凉一梦 提交于 2019-12-01 07:28:27
问题 I created this simple set of data to illustrate my point. It is a simple model with no further relations to any other model. I need to group the data above by topicid, find the max date for each group and then get the author for that date. info = TempModel.objects .values('topic') .annotate( max=Max('date')) .order_by('-max') Iterating through this in a template, <table> {% for item in info %} <tr> <td>{{ item.topicid }}</td> <td>{{ item.max }}</td> <td>{{ item.author }}</td> </tr> {% endfor

Django : select_related with ManyToManyField

荒凉一梦 提交于 2019-11-29 03:08:23
I have : class Award(models.Model) : name = models.CharField(max_length=100, db_index=True) class Alias(models.Model) : awards = models.ManyToManyField('Award', through='Achiever') class Achiever(models.Model): award = models.ForeignKey(Award) alias = models.ForeignKey(Alias) count = models.IntegerField(default=1) How can I have an Alias which has all its achiever_set and awards prepopulated? >>> db.reset_queries() >>> Alias.objects.filter(id="450867").select_related("achiever_set__award").get().achiever_set.all()[0].award.name u'Perma-Peddle' >>> len(db.connection.queries) 3 >>> db.reset

A left outer reverse select_related in Django?

吃可爱长大的小学妹 提交于 2019-11-28 09:38:21
Imagine the following model: class Parent(Model): ... class Child(Model) father = ForeignKey(Parent) ... Some parents have children, others do not (they're not parents in the real meaning, it's just a fictional name). I would like to make the following query: I want to list all the Parents , and if they have children, bring me the children too . That would be the equivalent of a left outer join to Child table, that is: select * from app_parent left join app_child on child_father_id=parent_id This way, when I invoke Parent.child_set in my template, I won't hit the database a gazillion times. Is