umutbozkurt / django-rest-framework-mongoengine

Mongoengine support for Django Rest Framework
MIT License
616 stars 166 forks source link

DictField: help_text not taken into account #225

Closed Vayel closed 7 years ago

Vayel commented 7 years ago

Probably the same problem than #224. I have this model:

class Model(Document):
    field = fields.MapField(fields.EmbeddedDocumentField('OtherModel'), help_text='help')

On the API, the help text does not appear whereas it does with a StringField for instance.

The help text does appear when I add it into extra_kwargs:

class Serializer(DocumentSerializer):
    class Meta:
        model = Model
        fields = '__all__'
        extra_kwargs = {'field': {'help_text': 'help'}}

Thanks!

Vayel commented 7 years ago

@BurkovBA can I help here?

BurkovBA commented 7 years ago

I'm actually surprised that this is happening - it shouldn't. Here is parsing of help_text argument: https://github.com/umutbozkurt/django-rest-framework-mongoengine/blob/master/rest_framework_mongoengine/utils.py#L125.

If you could create a unit-test, confirming this issue, it would be very nice, thank you!

For instance, you could modify either test_basic or test_compound, adding help_text argument to one of the fields:

Vayel commented 7 years ago

Indeed, it works. I am sorry, I redefined the field in my serializer so the attributes of the model were not used. To remedy that, I used extra_kwargs instead of redefinition:

class Collection(Document):
    posts = fields.MapField(
        fields.EmbeddedDocumentField('Post'),
        blank=True,
        help_text='Help!',
    )

class CollectionSerializer(mongoserializers.DocumentSerializer):
    # Before:
    # posts = mongofields.DictField(child=PostSerializer())

    class Meta:
        model = models.Collection
        fields = '__all__'
        extra_kwargs = {
            'posts': {
                'child': PostSerializer(),
                'validators': [validate_posts_dict],
            }
        }