Kris Tryber
Kris Tryber

Reputation: 237

Having problems with a query in Django

I'm building my first app within Django and I'm trying to query a "load" based on the company that i've attached to the load. Here's the model in question.

class Load(models.Model):
    company = models.ForeignKey(UserCompany, null=True, 
    on_delete=models.CASCADE)
    load_number = models.IntegerField()
    carrier = models.CharField(max_length=255)
    pickup_date = models.DateField()
    delivery_date = models.DateField()
    shipper = models.CharField(max_length=255)
    consignee = models.CharField(max_length=255)
    po_number = models.CharField(max_length=255)
    pu_number = models.CharField(max_length=255)
    pieces = models.IntegerField()
    description = models.TextField()
    date_created = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.date_created = timezone.now()
        self.save()

    def __str__(self):
        return str(self.load_number)

Now I'm trying to display a list on a page, but only display the loads attached to a specific company. The user needs to be attached to that company as well so here are my user models.

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class UserCompany(models.Model):
    company_name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.company_name

    def __str__(self):
        return self.company_name

# User Model
class UserProfileInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    # Additional Classes
    profile_pic = models.ImageField(upload_to='profile_pics', 
    blank=True)
    company = models.ForeignKey(UserCompany, 
    null=True,on_delete=models.CASCADE)

    def __str__(self):
        return self.user.username

Then i'm trying to query the "loads" within this view.

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render, get_object_or_404
from django.shortcuts import redirect
import datetime
from django.conf import settings
from django.utils import timezone
from django.http import HttpResponse
from django.views.generic import View, DetailView
from easy_pdf.views import PDFTemplateResponseMixin
from loads.models import Load
from .forms import LoadForm
from users.models import UserCompany, UserProfileInfo

# Create your views here.

class PDFUserDetailView(PDFTemplateResponseMixin, DetailView):
    model = Load
    template_name = 'loads/load_pdf.html'

def load_list(request):
    loads = 
Load.objects.filter(company=request.company).order_by('date_created')
    return render(request, 'loads/load_list.html', {'loads':loads})

I was able to get the query working based on the user, so my thought is that this query would be the same. This is not the case. I have a feeling that i'm referencing company wrong and maybe I somehow need to filter 2 more levels down all the way to the original UserCompany class, just not sure how to accomplish that.

The error that i'm getting is:

AttributeError: 'WSGIRequest' object has no attribute 'company'

Upvotes: 1

Views: 52

Answers (1)

Mathyou
Mathyou

Reputation: 653

request.company isn't a thing you can do. Instead of

Load.objects.filter(company=request.company).order_by('date_created')

You try something like this:

current_user = request.user
company = current_user.userprofileinfo.company
Load.objects.filter(company=company).order_by('date_created')

request just holds information about the current request which you can read more about here

Upvotes: 2

Related Questions