hY8vVpf3tyR57Xib
hY8vVpf3tyR57Xib

Reputation: 3915

Django models: getting users from object

I just started with Django, I love it! It still is quite challenging however. I have a model Shift that has users assigned to it with the model Assign. I want to get all the users that are assigned to a certain shift, but I can only get the Assign objects. Do you guys know how I should change my query?

The problem above gives me these 2 models:

class Shift(models.Model):
    shift_location = models.CharField(max_length=200)

    def get_shift_users(self):
        Assign.objects.filter(shift=self)


class Assign(models.Model):
    shift = models.ForeignKey(Shift, unique=False)
    user = models.ForeignKey(User, unique=False)

How do I return all users with the method get_shift_users, instead of all Assign objects? I thought that

Assign.objects.filter(shift=self).user

Would work...

Upvotes: 0

Views: 65

Answers (1)

levi
levi

Reputation: 22697

Starting from User

def get_shift_users(self):
   return User.objects.filter(assign_set__shift=self)

Upvotes: 1

Related Questions