Questions tagged [django-select-related]

Django operator, which returns a queryset that follows and caches foreign key relationships, in order to avoid hitting the database in future calls that require use of foreign keys.

117 questions
69
votes
3 answers

Django Query Related Field Count

I've got an app where users create pages. I want to run a simple DB query that returns how many users have created more than 2 pages. This is essentially what I want to do, but of course it's not the right…
Brenden
  • 6,473
  • 14
  • 39
  • 70
37
votes
3 answers

Selecting specific fields using select_related in Django

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…
33
votes
5 answers

A left outer reverse select_related in Django?

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…
augustomen
  • 7,153
  • 3
  • 35
  • 58
26
votes
2 answers

Django : select_related with ManyToManyField

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 =…
Paul Tarjan
  • 44,540
  • 54
  • 163
  • 207
21
votes
4 answers

Optimizing database queries in Django REST framework

I have the following models: class User(models.Model): name = models.Charfield() email = models.EmailField() class Friendship(models.Model): from_friend = models.ForeignKey(User) to_friend = models.ForeignKey(User) And those models…
14
votes
3 answers

django select_related for multiple foreign keys

How does select_related work with a model which has multiple foreign keys? Does it just choose the first one? class Model: fkey1, fkey2, fkey3... The documentation doesn't say anything about this, at least not in where the method is specified. NOTE:…
dtc
  • 1,608
  • 2
  • 20
  • 40
9
votes
2 answers

Update Django from 1.6 to 1.8: Invalid field name(s) given in select_related

I do update of project from Django 1.6.7 to 1.8.7 and I have got following exception with Django 1.8, although with Django 1.6 it code was right: In[2]: from apps.route import models In[3]: models.Trace.objects.select_related("trace_points") Out[3]:…
Peter
  • 981
  • 2
  • 9
  • 19
9
votes
1 answer

DJANGO: How to sort objects based on attribute of a related model?

I have a User model and UserProfile model. In the User model I'd like to order my query so that its in alphabetical order by last_name. Then I'd like to order it by the User_profiles "title" attribute (Manager, Executive, Accountant etc).…
thedeepfield
  • 5,814
  • 23
  • 68
  • 104
8
votes
1 answer

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

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…
Chris W.
  • 32,518
  • 30
  • 89
  • 121
7
votes
2 answers

Django select_related on chained foreign keys

I read the documentation and all the related questions here but I haven't properly understood how select_related behaves on chained/multiple foreign keys. Assume we have the following models: class RecordLabel(models.Model): title =…
Fotis Sk
  • 113
  • 1
  • 9
7
votes
2 answers

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

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 =…
basilikum
  • 9,714
  • 4
  • 38
  • 55
6
votes
1 answer

Django ORM - select_related and order_by with foreign keys

I have a simple music schema: Artist, Release, Track, and Song. The first 3 are all logical constructs while the fourth (Song) is a specific instance of an (Artist, Release, Track) as an mp3, wav, ogg, whatever. I am having trouble generating an…
Rob Crowell
  • 1,349
  • 2
  • 14
  • 23
5
votes
2 answers

Django select_related with fields specified breaks over multiple one to one relationships

I'm getting a weird error trying to select_related over multiple OneToOneField relationships, e.g. in the case where the target field is a grandchild subclass. I'd love someone to help me understand what's going on (or confirm that this is a bug in…
5
votes
1 answer

Non-relational field given in select_related: ' '. Choices are: (none)

I have two models from different apps: class Measure(models.Model): date = models.DateTimeField(default="2018-01-23 15:55") average = models.FloatField(default=0) class Sensor(models.Model): measure=models.ForeignKey(Measure,…
Alvaro
  • 1,146
  • 2
  • 17
  • 33
5
votes
1 answer

How to prefetch a @property with a Django queryset?

I would like to prefetch a model property to a queryset in Django. Is there a way do that ? Here are the three models: class Place(models.Model): name = models.CharField(max_length=200, blank=True) @property def bestpicurl(self): …
Paul Noon
  • 516
  • 5
  • 21
1
2 3 4 5 6 7 8