drf-forms/drf-schema-adapter

Related endpoints

Closed this issue · 4 comments

Hi,
Related endpoints generated by AutoMetadataMixin are different from the ones generated by the automatic registration of endpoints.
The firsts are simply using the model_name (singular) when the seconds are automatically inflected.

Wouldn't it make more sense to generate them the same way ? Because the related_endpoint link so far doesn't represent any real resource.

Hi,
I'm not sure what you mean. generated endpoints urls are inflected by drf-schema adapter. Maybe you have a code example?

Also, what versions of Dango, DRF and python are you using?

And also, which adapter are you using?

Sorry, I am using Django 1.11.11, DRF 3.8.1 python 3.6.3 and last version of drf-schema 0.9.55).
I am using a custom Adapter to actually overcome this issue (for me). But you can reproduce it with the BaseAdapter by example:
In utils.py the 'related_endpoint' field generated by the AutoMetadataMixin is generated this way:

        if related_model is not None:
            rv['related_endpoint'] = '{}/{}'.format(
                related_model._meta.app_label,
                related_model._meta.model_name.lower()
            )

When endpoints url/name are generated thanks to these properties:

    @property
    def singular_model_name(self):
        return self.model._meta.model_name.lower()

    @property
    def model_name(self):
        return self.inflector.pluralize(self.singular_model_name)

Hence for a model named 'Brand' the 'related_endpoint' field will be 'app_name/brand' when the actual endpoint generated is 'app_name/brands'

Ok, I see what you mean now :-)

So this is done this way because it is how Ember expects it and it is the main frontend I use. But your point is totally valid and I see why it would cause problem for clients other than Ember.

I guess the best way to address the problem would be to populate related_endpoint with more generic data (ie: a dictionary with app and model, maybe model_plural too) and then let each adapter do what it sees fit with that data.

Would you be willing to make a PR with those changes?