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