Expand not working as expected
See original GitHub issueI 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:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rsinger86 I’d add to this, that it can be confusing because of the implementation of
_should_field_exist
means that if you use bothfields
, andexpand
, you need to explicitly add all items you want to expand tofields
as well, otherwise they won’t be returned.In a way I feel like
expand
fields should be included infields
automatically rather than have to specify them twice, but in a sense I get how it is with the expected use case offields=object
returningobject.id
and requests withfields=object&expand=object
returning the full Object serialized. I guess what I suggest has negative quirks such asexpand=object
with nofields
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).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.
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.