-1

I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in user.

views.py

def get_queryset(self):
        assetlist = Asset.objects.filter(organisation__employee__user=self.request.user)
        print(assetlist)
        return assetlist

models.py

class Employee(models.Model):
    name = models.CharField("Employee Name", max_length=50, blank=False)
    email = models.CharField("Employee Email", max_length=50, blank=True)
    user = models.ForeignKey(User)
    clientID = models.ForeignKey(Organisation)

    def save(self):
        self.name = self.user.get_full_name()
        self.email = self.user.email
        super(Employee, self).save()

    def __str__(self):
        return self.name


class Asset(models.Model):
    name = models.CharField("Asset Name", max_length=30, primary_key=True)
    organisation = models.ForeignKey(Organisation)

    def __str__(self):
        return self.name


class Organisation(models.Model):
    name = models.CharField("Organisation Name", max_length=50, blank=False)
    location = models.TextField("Organisation Address", max_length=200, blank=True)
    tel = models.CharField("Telephone Number", max_length=20)

    def __str__(self):
        return self.name
M.javid
  • 6,387
  • 3
  • 41
  • 56
Brian Pace
  • 39
  • 1
  • 9
  • 3
    Contrary to the two answers below, this is the correct query for these models. What happens when you try this? If you're not getting any output at all in the console, it would imply that your `get_queryset` method is not being called. You should post the whole view. – Daniel Roseman Aug 18 '15 at 09:13
  • The use of `get_queryset` makes me think at [class-based generic views](https://docs.djangoproject.com/en/dev/topics/class-based-views/). Are you using them? If so, could you paste some more code? – Andrea Corbellini Aug 18 '15 at 10:06

3 Answers3

-1

There is no employee field inside organisation. It's an reversed relation, there are many employees attached so you can't query it like that.

But there is something called related_name in django foreign keys, with use of that, your query should look like that:

    assetlist = Asset.objects.filter(organisation__employee_set__user=self.request.user)

or if you specify your own related_name into employee -> organisation relation:

clientID = models.ForeignKey(Organisation, related_name="employees")

it will look like this:

    assetlist = Asset.objects.filter(organisation__employees__user=self.request.user)
GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
-1

The answer was to approach from another model, as seen below:

assetlist = Sensor.objects.filter(asset__organisation__employee__user=self.request.user)
Brian Pace
  • 39
  • 1
  • 9
-2

You have written wrong code. You want an Asset object by using:

assetlist = Asset.objects.filter(organisation__employee__user=self.request.user)

But you clearly can see in your models.py that there is no relationship between Organisation and Employee, then how can you get a result using organisation__employee...?

You should first create a ForeignKey field to relate with Employee model then your code will work fine.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Ashok Joshi
  • 428
  • 4
  • 13
  • The ForeignKey already exists: `clientID = models.ForeignKey(Organisation)` – Andrea Corbellini Aug 18 '15 at 10:26
  • but this is a relationship from Employee to Organisation...there is not any field in "Organisation" model which has the relation to the Employee model...like **employee = models.ForeignKey(Employee)** – Ashok Joshi Aug 18 '15 at 10:59
  • 1
    This is a reverse relationship lookup: https://docs.djangoproject.com/en/1.8/topics/db/queries/#lookups-that-span-relationships (In the example, note that `Blog` has not `entry` foreign key, but still the query works.) – Andrea Corbellini Aug 18 '15 at 11:17