djangomongodbdjango-rest-frameworkmongoenginelistfield

Only lists and tuples may be used in a list field Validation Error


Hi I am implementing test cases for my models. I am using Mongoengine0.9.0 + Django 1.8

My models.py

class Project(Document):
    # commented waiting for org-group to get finalize
    project_name = StringField()
    org_group = ListField(ReferenceField(OrganizationGroup, required=False))

My Serializers.py

class ProjectSerializer(DocumentSerializer):

    class Meta:
        model = Project
        depth = 1

test.py file

def setUp(self):
    # Every test needs access to the request factory.
    self.factory = RequestFactory()
    self.user = User.objects.create_user(
        username='jacob', email='jacob@jacob.com', password='top_secret')



def test_post_put_project(self):
    """
    Ensure we can create new clients in mongo database.
    """
    org_group = str((test_utility.create_organization_group(self)).id)
    url = '/project-management/project/'
    data = {
        "project_name": "googer",
        "org_group": [org_group],
    }
    ##import pdb; pdb.set_trace()

    factory = APIRequestFactory()
    user = User.objects.get(username='jacob')
    view = views.ProjectList.as_view()

    # Make an authenticated request to the view...
    request = factory.post(url, data=data,)
    force_authenticate(request, user=user)
    response = view(request)
    self.assertEqual(response.status_code, 200)

When I am running test cases I am getting this error

(Only lists and tuples may be used in a list field: ['org_group'])

The complete Stack Trace is

ValidationError: Got a ValidationError when calling Project.objects.create(). This may be because request data satisfies serializer validations but not Mongoengine`s. You may need to check consistency between Project and ProjectSerializer.

If that is not the case, please open a ticket regarding this issue on https://github.com/umutbozkurt/django-rest-framework-mongoengine/issues

Original exception was: ValidationError (Project:None) (Only lists and tuples may be used in a list field: ['org_group'])

Not getting why we cant pass object like this.

Same thing when I am posting as an request to same method It is working for me but test cases it is failing


Solution

  • The tests should be running using multipart/form-data, which means that they don't support lists or nested data.

    You can override this with the format argument, which I'm guessing you probably want to set to json. Most likely your front-end is using JSON, or a parser which supports lists, which explains why you are not seeing this.