rsinger86/drf-flex-fields

RecursionError when initialising FlexSerializer with expand kwarg

Opened this issue · 1 comments

I have this simple model:

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(
        "books.Author",
        verbose_name=_("Author of the book"),
        on_delete=models.CASCADE
    )
    publication_date = models.DateField()
    isbn = models.CharField(max_length=13)
    pages = models.IntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    publisher = models.ForeignKey(
        Publisher,
        verbose_name=_("Publisher of the book"),
        on_delete=models.CASCADE,
        null=True
    )

And this is how the FlexSerializer looks:

class BookSerializer(FlexFieldsModelSerializer):

    class Meta:
        model = Book
        fields = ['id', 'title',
                  'author', 'publication_date', 'isbn', 'pages',
                  'created_at', 'updated_at', 'publisher']
        read_only_fields = ["id", 'created_at', 'updated_at']

        expandable_fields = {
            'author': AuthorSerializer,
            'publisher': PublisherSerializer
        }

When I use BookSerializer(books, many=True, fields=["author"], expand=["author"]), I get a RecursionError: "maximum recursion depth exceeded".

If I remove the expand parameter or just use fields, it returns the serializer instance. I'd like to know if this is expected behavior or a bug.

Adding the repr method with custom representation to the flex serializer mixin solves the issue.