Django getting database fields with one query
NickName:Boky Ask DateTime:2017-03-03T19:32:32

Django getting database fields with one query

I have a few models in django (User(standard), Member(extended user), Calculations, Subscription, Type) as follows :

class Member(models.Model):
    user = models.ForeignKey(to=User)
    ..........

class Calculations(models.Model):
    user = models.ForeignKey(to=User)
    .....

class Type(models.Model):
    limit = models.IntegerField()
    name = models.CharField(max_length=50)
    default = models.BooleanField()


class Subscription(models.Model):
    started = models.DateField()
    type = models.ForeignKey(Type)
    member = models.OneToOneField(Member)

Thus,

  • Calculations is connected to User with ForeignKey
  • Member is also connected to user with ForeignKey
  • Subscriptions is connected to Member and with Type with ForeignKey

In my view I want to query the database and to access information from all those models.

Thus I want all members except the currently logged in, and I want to get the number of the calculations and the type of the subscription.

I'm trying to do it with one query.

I tried something like :

@login_required
def members(request):
    // That's how I get all information about user and members, but I also need the calculation and subscription
    members_list = Member.objects.select_related('user').all().exclude(user=request.user)
    context = {'members': members_list}
    return render(request, 'master/members.html', context)

Any advice how can I do that?

Copyright Notice:Content Author:「Boky」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42577791/django-getting-database-fields-with-one-query

More about “Django getting database fields with one query” related questions

Django getting database fields with one query

I have a few models in django (User(standard), Member(extended user), Calculations, Subscription, Type) as follows : class Member(models.Model): user = models.ForeignKey(to=User) ..........

Show Detail

Is it posible to compare a variable to two accumalated database fields in a django query?

I am trying to filter the results of a django query according to a given end and start time. In my model I have a start time and duration for every database entry. I give the function the parameters

Show Detail

Django. Database query: distinct for one field

I have following fields and data in DB: FirstName LastName Date John          Davis       10-10-2011 Joe      &

Show Detail

Django dynamic fields view

This post answers how to dynamically return a subset of fields based on the field section in the URL. This is done by creating a dynamic serializer. This works great against a ModelViewSet, but wil...

Show Detail

Django adminsite customize search_fields query

In the django admin you can set the search_fields for the ModelAdmin to be able to search over the properties given there. My model class has a property that is not a real model property, means it ...

Show Detail

Setting database level defaults for fields in django?

What methods do you use to set up database level defaults in django? I understand this is a django wontfix decision, but there must be some ideas floating around about a way to implement this if w...

Show Detail

how to validate django custom database fields?

I'm reading the django documentation on how to create custom database fields. However, there is no mention of how fields are validated. I looked into the source code of existing fields, but I can't...

Show Detail

Django database migration: merging two fields into one

Say I have a model that represents a product from my company. Each product a product number (ex. 5928523), as well as a boolean representing whether or not it is an experimental product. class Pro...

Show Detail

Multiplying two fields in django models to get total value but total value not getting updated in the database

My model looks like this. Other fields are getting updated in the database just as expected. The problem is getting the total value stored in the database by multiplying the price_per_unit with ...

Show Detail

limit django fields queried in sql call to database by queryset

I have a table with a blob and would like to exclude it from being in the sql call to the database unless specifically called for. Out of the box django includes everything in the queryset. So fa...

Show Detail