Repository: django-json-api/django-rest-framework-json-api

Issue:
Including lists of resources from non-model annotated objects
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 of `Product` 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 in `included`. I tried the following:

```python

class OrganizationSerializer(serializers.ModelSerializer):
    included_serializers = {
        "current_cycle": ProductSerializer,
        "next_cycle": ProductSerializer,
    }

    current_cycle = ResourceRelatedField(
        many=True, read_only=True, model=Product
    )
    next_cycle = ResourceRelatedField(
        many=True, read_only=True, model=Product
    )

    class Meta:
        model = Organization
        fields = ("pk", "current_cycle", "next_cycle")

    class JSONAPIMeta:
        included_resources = ("current_cycle", "next_cycle")
```
But this results in `AttributeError: type object 'Organization' has no attribute 'current_cycle'`, which happens in `utils.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 the `child_relation` attr when the `many` is set to True. It does a `child` attribute which in my case wasn't there although `child_relation` is.

Doing a quick and dirty fix by including 
```python
    elif (
        hasattr(relation, "child_relation")
        and hasattr(relation.child_relation, "model"
    )):
        relation_model = relation.child_relation.model
```
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!



Discussion:
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:
```python
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](https://github.com/django-json-api/django-rest-framework-json-api/issues/704#issuecomment-532793048) and closing this issue therefore. If not please comment.
I think I'm running into this. The issue can be reproduced with:

```python
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:

```python
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 `ManyRelatedField` by adding the following method to the serializer:

```python
    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.

Resolving patch:
diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py
--- a/rest_framework_json_api/utils.py
+++ b/rest_framework_json_api/utils.py
@@ -215,6 +215,11 @@ def get_related_resource_type(relation):
                 return get_related_resource_type(parent_model_relation)
 
     if relation_model is None:
+        # For ManyRelatedFields on plain Serializers the resource_type
+        # cannot be determined from a model, so we must get it from the
+        # child_relation
+        if hasattr(relation, "child_relation"):
+            return get_related_resource_type(relation.child_relation)
         raise APIException(
             _("Could not resolve resource type for relation %s" % relation)
         )


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -243,6 +243,27 @@ class Meta:
     assert get_related_resource_type(field) == output
 
 
+@pytest.mark.parametrize(
+    "related_field_kwargs,output",
+    [
+        ({"queryset": BasicModel.objects}, "BasicModel"),
+        ({"queryset": BasicModel.objects, "model": BasicModel}, "BasicModel"),
+        ({"model": BasicModel, "read_only": True}, "BasicModel"),
+    ],
+)
+def test_get_related_resource_type_from_plain_serializer_class(
+    related_field_kwargs, output
+):
+    class PlainRelatedResourceTypeSerializer(serializers.Serializer):
+        basic_models = serializers.ResourceRelatedField(
+            many=True, **related_field_kwargs
+        )
+
+    serializer = PlainRelatedResourceTypeSerializer()
+    field = serializer.fields["basic_models"]
+    assert get_related_resource_type(field) == output
+
+
 class ManyToManyTargetSerializer(serializers.ModelSerializer):
     class Meta:
         model = ManyToManyTarget