-
Notifications
You must be signed in to change notification settings - Fork 301
Added the ability to specify a resource_name on Models #152
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
Added the ability to specify a resource_name on Models #152
Conversation
Turns out that it is fairly hacky to add a custom property to the Alternatives I can see right now is hooking into the existing |
65071e9
to
003f57a
Compare
@scottfisk I'm wondering if this realy is a good way to deal with the resource name. |
@jsenecal Yeah I don't really like the state this got to either. Unfortunately you can't add something like |
@scottfisk, since adding custom properties to Meta is impractical, what An example of 2) would be class MyModel(models.Model):
class Meta:
# my meta options
class JSONAPIMeta:
resource_name = 'custom_resource_name' On 27 November 2015 at 12:08, Jonathan Senecal [email protected]
|
I personally like option 2 a lot 👍 |
@leifurhauks Im open to updating this PR to approach 2 if the maintainers think that would be preferable. EDIT: Just saw @jsenecal's message. I'll modify this PR to do that. 👍 |
Thanks to both of you @scottfisk @leifurhauks |
A thought -- I see in the current PR all references to It may be appropriate to keep the original _meta.model.name as a fallback in case the |
@bierdybard Yeah that makes sense to me. I plan to do that when I update this PR (early this week). Keeps the fallback path basically the same as on master. |
How about the model |
Making |
@jerel One could use proxy models and then have different serializers to point to them... |
@jsenecal right, proxy models would work fine if we go that route. I tend to think of models as the core with serializers next and views at the outer layer. So the natural priority seems like view > serializer > model |
So the lookup order shall be what exactly ? I think it is important we discuss that in detail here. |
Here is where the conflict occurs: A |
BTW, this is the case where |
So, maybe we should drop ressource_name on serializers altother, for consistency ? |
what about a serializer that is not tied to a model? |
@mkohram How would you use |
@jsenecal I was thinking the reverse of that:
Typically it would get derived from the model unless there was a need to set it at the serializer or view level. @mkohram that makes sense. I hadn't thought of that. I guess we could either add |
@jerel That would work for me |
@jerel Just to be clear we are still talking about keeping |
@mkohram yes, I am |
@mkohram yes, I was also |
This makes sense to me too. Defining |
So I switched this over to using I don't know what all you want me to include in this PR but I think:
EDIT: Can't rebase off |
cd35c63
to
1f1c26e
Compare
replaced other usages of getting model type by __name__
This isnt required anymore as the get_resource_type calls handle this
3104433
to
e35353e
Compare
I think I have a good start. 8 tests up to now: Included document and relation type match for all combinations (4) The other cases should be simpler to write. |
Alright, pretty big update on the tests (thanks @mkohram) and a couple fixes. I would like some feedback on my solution to the serializer overriding model when used with include issue. At the moment I did fe0056d. I think this covers the weird edge case by always using a serializer's resource_type even if it is not being requested as part of the |
fec8a04
to
0939f42
Compare
0939f42
to
bd5a955
Compare
@scottfisk @mkohram thanks to both of you for your significant effort on this. It looks like the only trouble with the build is due to Django 1.7. I don't see the error right away but it looks like it's related to pagination. If you do |
bdbd85c
to
836391f
Compare
@jerel @mkohram Ughh, fixing that test in django 1.7 was incredibly annoying to track down. I ended up just refactoring the tests we added to use Also, is there any testing we should do on POST/PUTs. Is the |
The parser uses Thanks a lot for tracking that failure down and for the rest of the work here. @jsenecal any last words? If there's no objections in the meantime I'll merge after I get back from Christmas break |
Great work on this everyone and Merry Christmas :) @jerel this looks great to me so I'll merge right away! |
Added the ability to specify a resource_name on Models
Thanks everyone especially @scottfisk. This wasn't straightforward. Still missing a couple of tests I believe - combinations of |
@jsenecal Was this ever merged into master? |
@mkohram it looks like the original PR targeted the |
I mentioned this yesterday on issue #74 and figured I would just open a PR to get opinions on this change.
This would allow a
resource_name
to be specified on the model. This was required in certain cases (relations: this line) where no serializer or view could have overridden the resource_name. This would allow theresource_name
of a model to be overridden globally for that model.I haven't written any tests yet as I uncovered a lot of places the model
__name__
property is used, hence WIP.EDIT:
Test TODOs: