-
Notifications
You must be signed in to change notification settings - Fork 301
Including lists of resources from non-model annotated objects #704
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
Comments
Not 100% certain about your use case, but usually when using a plain python object the resource name should be defined on the serializer itself. could be something like this: class ProductSerializer(Seriaizer):
...
class Meta:
resource_name = 'products' Does that work for you or still an exception? If there is still an exception please paste the stack trace. |
As there hasn't been any feedback on this I assume this issue has been resolved with above answer and closing this issue therefore. If not please comment. |
I think I'm running into this. The issue can be reproduced with: from uuid import uuid4
from django.db import models
from rest_framework_json_api import serializers, utils
class MyModel(models.Model):
"""My model class."""
created = models.DateTimeField()
class JSONAPIMeta:
"""JSON:API meta information."""
resource_name = "my-models"
class NonModel:
"""Non model class."""
def __init__(self, *, pk=None, my_models):
"""Initialise the variables."""
if pk is None:
pk = str(uuid4())
self.pk = pk
self.my_models = my_models
class NonModelSerializer(serializers.Serializer):
"""NonModel serializer."""
my_models = serializers.ResourceRelatedField(
model=MyModel, queryset=MyModel.objects.all(), many=True
)
class JSONAPIMeta:
"""JSON:API meta information."""
resource_name = "non-models"
def create(self, validated_data):
"""Return an instance of NonModel."""
return NonModel(**validated_data) Using this serializers results in: APIException: Could not resolve resource type for relation ManyRelatedField(child_relation=ResourceRelatedField(model=<class 'MyModel'>, queryset=<QuerySet []>)) I've worked around the issue by hacking the model onto the def get_fields(self):
"""Set model on my_models ManyRelatedField."""
fields = super().get_fields()
fields["my_models"].model = fields["my_models"].child_relation.model
return fields |
@nattyg93 Thanks for the example. This could be that this is the issue indeed. I am reopening this issue. PR is welcome. |
I have a
Organization
model with a serializer whose instances get some extra data bound to then when using a specific view. this extra data is two lists ofProduct
objects, which is not a django model (just a plain old python object) but its representation should look like a regular relation: the attrs for the organization and then the product lists inincluded
. I tried the following:But this results in
AttributeError: type object 'Organization' has no attribute 'current_cycle'
, which happens inutils.get_related_resource_type
.Stepping through the code there, it seems like although it's checking the
model
attribute directly on the relation, it's not reaching into thechild_relation
attr when themany
is set to True. It does achild
attribute which in my case wasn't there althoughchild_relation
is.Doing a quick and dirty fix by including
in the big if/elif/else in
get_related_resource_type
seems to work. I'm however bery new to this library and maybe I'm just missing something. Otherwise I can make a PR with this fix, of course. Any help would be greatly appreciated :) Thanks!The text was updated successfully, but these errors were encountered: