noobmaster64
noobmaster64

Reputation: 85

How to chain queries in django view

Hello there is a specific problem that i am trying to solve. Im making a tasking system that is built on 3 models , the taskings model , the extended user model(profile) , and the django default User model. I wish to display the username of the people whom were assigned the taskings through the sales_extras field which is an extended model of the User model.

Here are my models

class Sales_taskings(models.Model):
sales_status= (
    ('p1','Phase 1'),
    ('p2','Phase 2'),
    ('p3','Phase 3'),
    ('p4','Phase 4'),
)
task_id = models.AutoField(primary_key=True)
sales_extras= models.ManyToManyField('sales.Sales_extras')
sales_project= models.ForeignKey('sales.Sales_project',on_delete=models.CASCADE)
description = models.TextField(max_length=200 , default='your notes' )
date_time = models.DateTimeField(auto_now=True)
status = models.TextField(max_length=10, choices= sales_status ,default='p1')


class Sales_extras(models.Model):
    role= (
        ('sm','sales_manager'),
        ('s','sales'),
    )
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    user_type = models.TextField(max_length=500, choices= role)

here is my view with context

    @login_required
def sales_taskboard(request):
    authentication_classes = [SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated]
    sales_extras_id = Sales_taskings.objects.values('sales_extras').get('username')
    print(Sales_taskings.objects.values('sales_extras'))
    usernames = User.objects.values('username').filter(id__in = sales_extras_id)
    print(usernames) 
    context = {
    'sales_task' : Sales_taskings.objects.all(),
    'sales_extras' : Sales_taskings.objects.values('sales_extras'),
    'usernames' : usernames
    }
    return render(request, 'sales/taskboard.html',context)

Sorry i am new to django , above is me trying ways to get the username out. I feel like there should be a built in query method that enables me to link my sales_takings model and my sales_extras model and finally to my User model .

Your help will be greatly appreciated!

Upvotes: 2

Views: 354

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32244

Every Sales_taskings instance has a ManyToManyField "sales_extras" which when accessed will give a queryset of all related Sales_extras. Since each Sales_extras has a relationship to User this can all be done using these attributes in your template

Context

context = {
    'sale_tasks' : Sales_taskings.objects.all()
}

Template

{% for sale in sale_tasks %}
    <h4>{{ sale.description }}</h4>
    <ul>
        {% for extra in sale.sales_extras.all %}
            <li>{{ extra.user }}</li>
        {% endfor %}
    </ul>
{% endfor %}

This will be fairly inefficient as you will perform a query for every sale and another query for every extra. You can use prefetch_related to reduce this

context = {
    'sale_tasks' : Sales_taskings.objects.prefetch_related('sales_extras__user')
}

Upvotes: 1

Related Questions