[Answered ]-Multiple ManyToOne relations serializer

1👍

You can span a ManyToManyField [Django-doc] over you BookMember model:

from django.conf import settings

class Book(TimeStampedModel):
    # …
    owner = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.DO_NOTHING
    )
    members = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        through='BookMember'
    )
    # …
    
    class Meta:
        db_table = 'books'


class BookMember(TimeStampedModel):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.DO_NOTHING
    )
    book = models.ForeignKey(
        Book,
        on_delete=models.CASCADE
    )
    
    class Meta:
        db_table = 'book_member'

Then you can serialize with:

class BookSerializer(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True, many=True, required=False)
    members = UserDetailSerializer(read_only=True, many=True, required=False)

    class Meta:
        model = Book
        fields = '__all__'

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.


Note: Usually it is better to work with a OneToOneField [Django-doc]
instead of a ForeignKey that has unique=True. A OneToOneField is a ForeignKey with unique=True and some small
modifications: for example it uses by default the name of the model in lowercase as
related_name=… [Django-doc]
and makes accessing the relation in reverse more convenient since that does not require a manager in between.


Note: Specifying null=False [Django-doc] is not necessary: fields are by default not NULLable.

Leave a comment