It sounds simple enough: get all the Users for a specific Company. But it is complicated by a few things:
- The User model is extended
- The model that extends it contains a UUID that uniquely identifies the user throughout various system integrations
- This UUID is what is used in the company relationship.
So the relationship is user_to_company__user_uuid
-> user_extended__user_id
-> auth_user
. That's what I'd like to return is the User model.
What I have:
# url
/api/user_to_company/?company_uuid=0450469d-cbb1-4374-a16f-dd72ce15cf67
# views.py
class UserToCompanyViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
filter_backends = [StrictDjangoFilterBackend]
filterset_fields = [
'id',
'user_uuid',
'company_uuid'
]
permission_classes = [CompanyPermissions]
def get_queryset(self):
if self.request.GET['company_uuid']:
queryset = User.objects.filter(
user_to_company__company_uuid=self.request.GET['company_uuid'])
return queryset
def get_serializer_class(self):
if self.request.GET['company_uuid']:
serializer_class = UserSerializer
return serializer_class
# serializers.py
class UserToCompanySerializer(serializers.ModelSerializer):
class Meta:
model = UserToCompany
fields = '__all__'
class UserExtendedSerializer(serializers.ModelSerializer):
class Meta:
model = UserExtended
fields = '__all__'
# models.py
class UserExtended(models.Model):
user_id = models.OneToOneField(
User, on_delete=models.CASCADE, db_column='user_id')
uuid = models.UUIDField(primary_key=True, null=False)
class Meta:
db_table = 'user_extended'
class UserToCompany(models.Model):
user_uuid = models.ForeignKey(
UserExtended, on_delete=models.CASCADE, db_column='user_uuid', related_name='user_to_company')
company_uuid = models.ForeignKey(
Companies, on_delete=models.CASCADE, db_column='company_uuid', related_name='user_to_company')
class Meta:
db_table = 'user_to_company'
unique_together = [['user_uuid', 'company_uuid']]
Understandably, in this setup User.object.filter(user_to_company__company_uuid=self.reqest.GET['company_uuid']
doesn't make sense and it returns django.core.exceptions.FieldError: Cannot resolve keyword 'user_to_company' into field
--There isn't a relationship between User and UserToCompany directly, but there is between User -> UserExtended -> UserToCompany
I could like do this by using UserExtended.object.filter()
, but that returns an object like :
[
{
"user_extended_stuff_1": "stuff",
"user_extended_stuff_2": "more stuff",
"auth_user": {
"auth_user_stuff_1": "stuff",
"auth_user_stuff_2": "more stuff"
}
}
]
But I need an object like:
[
{
"auth_user_stuff_1": "stuff",
"auth_user_stuff_2": "more stuff",
"user_extended": {
"user_extended_stuff_1": "stuff",
"user_extended_stuff_2": "more stuff"
}
}
]
Is there a way to implement "foreign key of a foreign key" lookup?
I think a work around would get the list of users and then do something like User.objects.filter(user_ext__user_uuid__in=[querset])