Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated BaseGenericInlineFormSet to match the changes made to BaseInl…

…ineFormSet in r11874.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11885 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fd4cc65baf7055e7815513756a3d3f6940c1d2ab 1 parent c0c6e7d
Russell Keith-Magee authored
29  django/contrib/contenttypes/generic.py
@@ -292,15 +292,27 @@ class BaseGenericInlineFormSet(BaseModelFormSet):
292 292
     ct_field_name = "content_type"
293 293
     ct_fk_field_name = "object_id"
294 294
 
295  
-    def __init__(self, data=None, files=None, instance=None, save_as_new=None, prefix=None):
  295
+    def __init__(self, data=None, files=None, instance=None, save_as_new=None,
  296
+                 prefix=None, queryset=None):
  297
+        # Avoid a circular import.
  298
+        from django.contrib.contenttypes.models import ContentType
296 299
         opts = self.model._meta
297 300
         self.instance = instance
298 301
         self.rel_name = '-'.join((
299 302
             opts.app_label, opts.object_name.lower(),
300 303
             self.ct_field.name, self.ct_fk_field.name,
301 304
         ))
  305
+        if self.instance is None or self.instance.pk is None:
  306
+            qs = self.model._default_manager.none()
  307
+        else:
  308
+            if queryset is None:
  309
+                queryset = self.model._default_manager
  310
+            qs = queryset.filter(**{
  311
+                self.ct_field.name: ContentType.objects.get_for_model(self.instance),
  312
+                self.ct_fk_field.name: self.instance.pk,
  313
+            })
302 314
         super(BaseGenericInlineFormSet, self).__init__(
303  
-            queryset=self.get_queryset(), data=data, files=files,
  315
+            queryset=qs, data=data, files=files,
304 316
             prefix=prefix
305 317
         )
306 318
 
@@ -312,19 +324,6 @@ def get_default_prefix(cls):
312 324
         ))
313 325
     get_default_prefix = classmethod(get_default_prefix)
314 326
 
315  
-    def get_queryset(self):
316  
-        # Avoid a circular import.
317  
-        from django.contrib.contenttypes.models import ContentType
318  
-        if self.instance is None or self.instance.pk is None:
319  
-            return self.model._default_manager.none()
320  
-        qs = self.model._default_manager.filter(**{
321  
-            self.ct_field.name: ContentType.objects.get_for_model(self.instance),
322  
-            self.ct_fk_field.name: self.instance.pk,
323  
-        })
324  
-        if not qs.ordered:
325  
-            qs = qs.order_by(self.model._meta.pk.name)
326  
-        return qs
327  
-
328 327
     def save_new(self, form, commit=True):
329 328
         # Avoid a circular import.
330 329
         from django.contrib.contenttypes.models import ContentType
4  tests/regressiontests/generic_inline_admin/models.py
@@ -20,7 +20,7 @@ def __unicode__(self):
20 20
 
21 21
 class MediaInline(generic.GenericTabularInline):
22 22
     model = Media
23  
-    
  23
+
24 24
 class EpisodeAdmin(admin.ModelAdmin):
25 25
     inlines = [
26 26
         MediaInline,
@@ -56,7 +56,7 @@ class MediaMaxNumInline(generic.GenericTabularInline):
56 56
     model = Media
57 57
     extra = 5
58 58
     max_num = 2
59  
-    
  59
+
60 60
 admin.site.register(EpisodeMaxNum, inlines=[MediaMaxNumInline])
61 61
 
62 62
 #
58  tests/regressiontests/generic_inline_admin/tests.py
@@ -17,7 +17,7 @@ def setUp(self):
17 17
         self.original_template_debug = settings.TEMPLATE_DEBUG
18 18
         settings.TEMPLATE_DEBUG = True
19 19
         self.client.login(username='super', password='secret')
20  
-        
  20
+
21 21
         # Can't load content via a fixture (since the GenericForeignKey
22 22
         # relies on content type IDs, which will vary depending on what
23 23
         # other tests have been run), thus we do it here.
@@ -25,26 +25,30 @@ def setUp(self):
25 25
         self.episode_pk = e.pk
26 26
         m = Media(content_object=e, url='http://example.com/podcast.mp3')
27 27
         m.save()
28  
-        self.media_pk = m.pk
29  
-    
  28
+        self.mp3_media_pk = m.pk
  29
+
  30
+        m = Media(content_object=e, url='http://example.com/logo.png')
  31
+        m.save()
  32
+        self.png_media_pk = m.pk
  33
+
30 34
     def tearDown(self):
31 35
         self.client.logout()
32 36
         settings.TEMPLATE_DEBUG = self.original_template_debug
33  
-    
  37
+
34 38
     def testBasicAddGet(self):
35 39
         """
36 40
         A smoke test to ensure GET on the add_view works.
37 41
         """
38 42
         response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/add/')
39 43
         self.failUnlessEqual(response.status_code, 200)
40  
-    
  44
+
41 45
     def testBasicEditGet(self):
42 46
         """
43 47
         A smoke test to ensure GET on the change_view works.
44 48
         """
45 49
         response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk)
46 50
         self.failUnlessEqual(response.status_code, 200)
47  
-    
  51
+
48 52
     def testBasicAddPost(self):
49 53
         """
50 54
         A smoke test to ensure POST on add_view works.
@@ -57,7 +61,7 @@ def testBasicAddPost(self):
57 61
         }
58 62
         response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/add/', post_data)
59 63
         self.failUnlessEqual(response.status_code, 302) # redirect somewhere
60  
-    
  64
+
61 65
     def testBasicEditPost(self):
62 66
         """
63 67
         A smoke test to ensure POST on edit_view works.
@@ -65,17 +69,45 @@ def testBasicEditPost(self):
65 69
         post_data = {
66 70
             "name": u"This Week in Django",
67 71
             # inline data
68  
-            "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"2",
69  
-            "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"1",
70  
-            "generic_inline_admin-media-content_type-object_id-0-id": u"%d" % self.media_pk,
  72
+            "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"3",
  73
+            "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"2",
  74
+            "generic_inline_admin-media-content_type-object_id-0-id": u"%d" % self.mp3_media_pk,
71 75
             "generic_inline_admin-media-content_type-object_id-0-url": u"http://example.com/podcast.mp3",
72  
-            "generic_inline_admin-media-content_type-object_id-1-id": u"",
73  
-            "generic_inline_admin-media-content_type-object_id-1-url": u"",
  76
+            "generic_inline_admin-media-content_type-object_id-1-id": u"%d" % self.png_media_pk,
  77
+            "generic_inline_admin-media-content_type-object_id-1-url": u"http://example.com/logo.png",
  78
+            "generic_inline_admin-media-content_type-object_id-2-id": u"",
  79
+            "generic_inline_admin-media-content_type-object_id-2-url": u"",
74 80
         }
75 81
         url = '/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk
76 82
         response = self.client.post(url, post_data)
77 83
         self.failUnlessEqual(response.status_code, 302) # redirect somewhere
78  
-    
  84
+
  85
+    def testGenericInlineFormset(self):
  86
+        EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, extra=3)
  87
+        e = Episode.objects.get(name='This Week in Django')
  88
+
  89
+        # Works with no queryset
  90
+        formset = EpisodeMediaFormSet(instance=e)
  91
+        self.assertEquals(len(formset.forms), 5)
  92
+        self.assertEquals(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="1" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>')
  93
+        self.assertEquals(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="2" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>')
  94
+        self.assertEquals(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="text" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>')
  95
+
  96
+        # A queryset can be used to alter display ordering
  97
+        formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.order_by('url'))
  98
+        self.assertEquals(len(formset.forms), 5)
  99
+        self.assertEquals(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="2" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>')
  100
+        self.assertEquals(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="1" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>')
  101
+        self.assertEquals(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="text" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>')
  102
+
  103
+
  104
+        # Works with a queryset that omits items
  105
+        formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png"))
  106
+        self.assertEquals(len(formset.forms), 4)
  107
+        self.assertEquals(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="2" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>')
  108
+        self.assertEquals(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>')
  109
+
  110
+
79 111
     def testGenericInlineFormsetFactory(self):
80 112
         # Regression test for #10522.
81 113
         inline_formset = generic_inlineformset_factory(Media,

0 notes on commit fd4cc65

Please sign in to comment.
Something went wrong with that request. Please try again.