umutbozkurt/django-rest-framework-mongoengine

list of references not getting saved

sukeshlaghate opened this issue · 1 comments

I have following models

class Connections(Document):
    url = fields.StringField(max_length=100, if_missing='', blank=True, default="http://localhost")
    user = fields.StringField(max_length=100, default='john.doe')
    password = fields.StringField(max_length=100, blank=True)
    impersonate_user = fields.BooleanField(default=False)
    impersonate_user_name = fields.StringField(max_length=100, default='', blank=True)
    impersonate_password = fields.StringField(max_length=100, default='', blank=True)
    proxy = fields.URLField(max_length=100, default='http://localhost', blank=True)
    connection_type = fields.IntField(max_length=1, choices=ConnectionTypes.choices())

    meta = {
        'indexes': [
            { 'fields': ['_id'] }
        ],
        'auto_create_index':False,
    }

class Languages(Document):
    language = fields.StringField()
    lang_code_i18n = fields.StringField(regex='^[a-z]{2}-[A-Z]{2}', required=True)
    meta = {
        'indexes': [
            { 'fields': ['lang_code_i18n'] }
        ],
        'auto_create_index': False,
    }


class BaseConfiguration(Document):
    user = fields.IntField(editable=False)
    languages = fields.ListField(fields.ReferenceField(Languages, related_name='configurations', dbref= True))
    connections = fields.ListField(fields.ReferenceField(Connections, related_name='configurations', dbref=True))
    meta = {
        'collection': 'configurations',
        'indexes': [
            { 'fields': ['user'] }
        ],
        'auto_create_index': False,
    }

and the serializer for BaseConfigurations

class ConfigurationSerializer( mongoserializers.DocumentSerializer ):
        class Meta:
            depth = 1
            model = BaseConfiguration
            fields = '__all__'

The view shows correct values for get requrests however when I try to create a new document by providing user value and object ids for languages & connections it creates empty document.

I tried to override the create method in the serializer. However, create method does not get called.

 def create(self, validated_data):
             languages = validated_data.pop("languages", None)
             connections = validated_data.pop("connections", None)
             print(languages)
             print(connections)
             instance =  super(ConfigurationSerializer, self).create(validated_data)
             print(instance)
             for language in Languages :
                 instance.languages.append(language)
             instance.save()
             for connection in connections:
                 instance.connections.append(connection)
             instance.save()
             return instance

Could you please suggest how can I create a new document?

@sukeshlaghate Hi!

First, I had to fix several fields in your Documents, as they are ok on django Models, but don't exist on Documents. So, you need to get rid of related_name attrs and also rename blank to null.

Second, I think that create() method of your Serializer doesn't get called because it is never reached - you get an error with your data on validation stage probably.

DRF calls create() method of your ViewSet (not Serializer!), which tries to perform validation of the data and supposedly fails: https://github.com/encode/django-rest-framework/blob/master/rest_framework/mixins.py#L18.