0

I'm currently working in my first Django project and am using the authentication mechanism that is included with Django. I currently have a game model with a foreign key relationship to a user in the auth_user table. Here are the following fields:

class Game(models.Model):
    game_id = models.UUIDField(default=uuid.uuid4, editable=False, max_length=10)
    host = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
    join_code = models.CharField(max_length=100, null=True)
    active = models.BooleanField(default=True)

I currently have a view method that just grabs all of the active game objects, and then passes those results into a serializer that serializes the data to be returned to the client.

def get(self, request, format=None):
    games = Game.objects.filter(active__exact=True)
    serializer = GameSerializer(games, many=True)
    return Response(serializer.data)

I want to add the username and email fields that are in AUTH_USER_MODEL to the results to be serialized, but I haven't been able to find examples of adding specific fields from a related model to the results that are serialized. Basically, I'm trying to figure out the django model equivalent of the following SQL statement

select u.username, u.email, g.* from game g
inner join AUTH_USER_MODEL u on u.user_id = g.host_id

Finally, once I have the results from that sort of query, how would I serialize the combined objects? Do I need to create another model that's a combination of the two models?

2 Answers2

0

I found what I was looking for, I can use a nested serializer, here's the stack overflow answer that pointed me in the right direction: Serialize data from multiple models django

Here is the Django rest framework documentation: https://www.django-rest-framework.org/api-guide/relations/#nested-relationships

0

Your serializer should look like this -

class AuthUserModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = AuthUserModel
        fields = ('username', 'email')

class GameSerializer(serializers.ModelSerializer):
    auth = AuthUserModelSerializer()

    class Meta:
        model = Game
        fields = '__all__'

Use your serializer where you want -

serializer = GameSerializer()

Now in your serializer response, you will get all the fields from game model and username,email from auth_user_model.

Mobasshir Bhuiya
  • 954
  • 6
  • 20
  • I'm using exactly that for my serializer, unfortunately I'm now running into this exception when hitting the API: "AttributeError: Got AttributeError when attempting to get a value for field `user` on serializer `GameSerializer`. web_1 | The serializer field might be named incorrectly and not match any attribute or key on the `Game` instance. web_1 | Original exception text was: 'Game' object has no attribute 'user'." – Patrick Peters Dec 11 '21 at 19:51
  • I think the exception is right because I can't see the `user` attribute on your GAME model. My provided code is just to give you an idea change things accordingly – Mobasshir Bhuiya Dec 11 '21 at 22:35