-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP for has many releationships with django-filter. #58
Conversation
if (relationship.kind === 'hasMany' && adapter.useHasManyRelatedFilterUrl) { | ||
// FIXME This is the wrong approach. Inverse maybe? | ||
var parentTypeKey = relationship.parentType.typeKey; | ||
if (parentTypeKey === 'trainerProductGroup') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hard-coded for one of my projects but it needs to be generalized for it to be useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the relationship.kind is 'hasMany', then we could safely assume that if hash['related_name'] begins with "/" or "http://" that it is a link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like,
if (relationship.kind === 'hasMany' && hash[payloadRelKey].match(/^\//)) {
// Add to links ...
}
I'd like to implement the hyper link fields feature. It's independent of the the django-filter solution so I can always work on that later. What do you think? |
Can you share a little of how your serializers are set up? I'm having some difficulty wrapping my head around this. |
Let me give you a simplified example. Product Group class ProductGroup(models.Model):
name = models.CharField(max_length=100)
products = models.ManyToManyField('Product', related_name='product_groups')
class ProductGroupSerializer(serializers.ModelSerializer):
class Meta:
model = ProductGroup
fields = ('id', 'name', 'products')
{id: 23, name: 'Fun Products', products: [12, 44, 234]} Product class Product(models.Model):
name = models.CharField(max_length=100)
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = ProductGroup
fields = ('id', 'name', 'product_group')
{id: 12, name: 'Silly string', product_group: 23} I think this is a pretty standard setup so far. The interesting bit is the ProductView with the DjangoFilterBackend. class ProductViewSet(viewsets.ModelViewSet):
serializer_class = ProductSerializer
queryset = Product.objects.all()
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('product_group',) Now we can make a request for the Products in ProductGroup 23 with this URL:
[
{id: 12, name: 'Silly string', product_group: 23},
{id: 44, name: 'Whoopee cushion', product_group: 23},
{id: 234, name: 'Stink bomb', product_group: 23},
] The code in this PR just tries to automatically build the It would be better to get DRF to generate the correct URL with a custom HyperLinkedField and just add general support for the HyperLinkedField to EDA. For example, the ProductGroup json would look like this if I wanted to use the django-filter pattern:
{id: 23, name: 'Fun Products', products: '/api/products/?product_group=23'} So, the next task on my TODO list for EDA is to add general support for HyperLinkedFields (without the logic to build the url with the query param). Does this sound reasonable to you? |
I am going to finish the work on emberjs/data#2468 so we can support coalesceFindRequests. Hope to do so by the end of the week. As things are now, (if you remove the block in the adapter) I've noticed that with This doesn't really relate to the hyperlinked fields discussion, but I think hasMany will get a lot easier once that pull request is finished. |
I'm surprised by that as well. How does ED know to build the query param url correctly? Maybe ED is loading all of the records, not just the related records. Did you check the console api requests to see if it's loading the related or all of the data? |
I was wrong on this. You are right, ED does build the query param URL correctly, but without a filter on the DRF side, DRF returns everything. So it works, but is not efficient to say the least. #68 clears this up by documenting how to enable coalesceFindRequests and add a custom filter to DRF. |
What do we have to do to finish this? |
I actually have this almost finished in another branch. I'm back to doing Ember stuff again so I'll prioritize getting this done. |
I did a bit of work on this today on another branch (with tests) and it looks like it's not going to need a custom Hyperlinked serializer field. The problem is that the Post {
"id": 11,
"title": "post title 11",
"body": "post body 11",
"comments": [
"http://localhost:8000/api/comments/9/",
"http://localhost:8000/api/comments/10/",
"http://localhost:8000/api/comments/11/"
]
} It's possible to support the Comment {
"id": 9,
"body": "comment body 1 for post 11",
"post": "http://localhost:8000/api/posts/11/"
} The next task would be to create the custom Hyperlinked serializer field that generates one URL using the DjangoFilterBackend or using embedded URLs (e.g. |
Closing this PR in favour of #95. |
This shouldn't be merged.