Commits

Craig Blaszczyk  committed 3bdf0fb

remove hardcoded model._meta.many_to_many[0]; update mixin tests to test with 0, 1, or multiple groups

  • Participants
  • Parent commits fb1a9c4

Comments (0)

Files changed (2)

File djangorestframework/mixins.py

         for field in model._meta.many_to_many:
             if content.has_key(field.name):
                 m2m_data[field.name] = (
-                    model._meta.many_to_many[0].m2m_reverse_field_name(),
-                    content[field.name]
+                    field.m2m_reverse_field_name(), content[field.name]
                 )
                 del content[field.name]
 

File djangorestframework/tests/mixins.py

    
             def url(self, instance):
                 return "/customusers/%i" % instance.id
+            
+        form_data = {'username': 'bar0', 'groups': []}        
+        request = self.req.post('/groups', data=form_data)
+        cleaned_data = dict(form_data)
+        cleaned_data['groups'] = []
+        mixin = CreateModelMixin()
+        mixin.resource = UserResource
+        mixin.CONTENT = cleaned_data
 
-        group = Group(name='foo')
+        response = mixin.post(request)
+        self.assertEquals(1, CustomUser.objects.count())
+        self.assertEquals(0, response.cleaned_content.groups.count())            
+
+        group = Group(name='foo1')
         group.save()
 
-        form_data = {'username': 'bar', 'groups': [group.id]}        
+        form_data = {'username': 'bar1', 'groups': [group.id]}        
         request = self.req.post('/groups', data=form_data)
         cleaned_data = dict(form_data)
         cleaned_data['groups'] = [group]
         mixin.CONTENT = cleaned_data
 
         response = mixin.post(request)
-        self.assertEquals(1, CustomUser.objects.count())
+        self.assertEquals(2, CustomUser.objects.count())
         self.assertEquals(1, response.cleaned_content.groups.count())
-        self.assertEquals('foo', response.cleaned_content.groups.all()[0].name)        
+        self.assertEquals('foo1', response.cleaned_content.groups.all()[0].name)
+        
+        
+        group2 = Group(name='foo2')
+        group2.save()        
+        
+        form_data = {'username': 'bar2', 'groups': [group.id, group2.id]}        
+        request = self.req.post('/groups', data=form_data)
+        cleaned_data = dict(form_data)
+        cleaned_data['groups'] = [group, group2]
+        mixin = CreateModelMixin()
+        mixin.resource = UserResource
+        mixin.CONTENT = cleaned_data
+
+        response = mixin.post(request)
+        self.assertEquals(3, CustomUser.objects.count())
+        self.assertEquals(2, response.cleaned_content.groups.count())
+        self.assertEquals('foo', response.cleaned_content.groups.all()[0].name)
+        self.assertEquals('foo2', response.cleaned_content.groups.all()[1].name)