question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expand not working as expected

See original GitHub issue

I have the following models:

class UploadTemplate(models.Model):
    book = models.ForeignKey('Book', on_delete=models.SET_NULL, null=True)
    coupon_type = models.CharField(max_length=100)
class Book(models.Model):
    book_name = models.CharField(max_length=30)
    #....random other fields

and serializers as follows:

class TemplateSerializer(FlexFieldsModelSerializer):
    book = serializers.PrimaryKeyRelatedField(read_only=True)
    class Meta:
        model = UploadTemplate
        fields = ["coupon_type", "id", "book"]
        expandable_fields = {
            'book': ('BookSerializer', {
                'source': 'book'
            })
        }
class BookSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

If i make a call to http://localhost:8000/v1/coupons/templates/6/?expand=book I’d expect book to expand to the nested serializer. Unfortunately it doesn’t and I’ve drawn a blank debugging.

My viewset:

from rest_flex_fields.views import FlexFieldsMixin

class UploadTemplateViewset(viewsets.ModelViewSet, FlexFieldsMixin):
    queryset = models.UploadTemplate.objects.all()
    serializer_class = serializers.UploadTemplateSerializer
    filterset_fields = ('book', )
    permit_list_expands = ['book']

    def get_queryset(self):
        print(is_expanded(self.request, 'book'))
        return models.UploadTemplate.objects.all()

confirms that book is expanded. (i,e, it prints True in the console).

Am I doing something obviously dumb, and if not, where should I start with debugging the issue? I’m on django 2, python 3.6

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SamMorrowDrumscommented, Oct 15, 2019

@rsinger86 I’d add to this, that it can be confusing because of the implementation of _should_field_exist means that if you use both fields, and expand, you need to explicitly add all items you want to expand to fields as well, otherwise they won’t be returned.

In a way I feel like expand fields should be included in fields automatically rather than have to specify them twice, but in a sense I get how it is with the expected use case of fields=object returning object.id and requests with fields=object&expand=object returning the full Object serialized. I guess what I suggest has negative quirks such as expand=object with no fields specified would then return everything, plus an expanded object. Anyway, just mentioning the confusion, because it did get me for a while.

I had been using the expands to hide certain function fields that are database intensive (or that call external APIs, like auth token Amazon S3 file urls).

expandable_fields = {
            'company_logo': (serializers.CharField, {'source': 'company_logo'}),
            'link': (serializers.CharField, {'source': 'public_relative_url'}),
}

And it surprised me at first that I had to include them in the fields, as they are already in the expand list.

Maybe we could add to the docs, that when wanting expands while using the "fields" query you must specify both the "expand" and "fields" entry for example: fields=object&expand=object

Anyway, maybe it’s obvious to others, but I did lose some time over it.

0reactions
yezyilomocommented, May 10, 2019

I was making something like this but for GET method only, I think I’ve managed to make it more advanced and flexible, for those who want to check it out it’s django-restql. Feedback will be appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - HTML collapse/expand tree not working as expected
I want to collapse category tree with multi hierarchy. I tried so many answers from stack, but it's not working ...
Read more >
Expand not working as expected - Adobe Support Community
Solved: I have a design consisting of a triangle, a polygon and some lines. I want to convert this whole thing as a...
Read more >
Collapsible code macro inside expand block doesn't work as ...
The code block expands, and disappears as the outer expand is (incorrectly) collapsed. Workaround. Expand the outer expand macro again, to reveal the...
Read more >
$expand does not return the expected 20 members
We have several groups with >20 members, if the size of expanded members is 20, we call groups/{id}/members. This was working fine for...
Read more >
Snippets and Text Expansion Troubleshooting - Alfred
If you're unable to expand your snippet, check these preferences and quit or re-launch the appropriate app. Secure Input notice in snippets. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found