Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Updated BaseGenericInlineFormSet to match the changes made to…

… BaseInlineFormSet in r11874.

Backport of r11885 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11886 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit de6497009134cfe979556f64cab8ceaabc3c774d 1 parent 3071d09
Russell Keith-Magee authored December 17, 2009
29  django/contrib/contenttypes/generic.py
@@ -294,15 +294,27 @@ class BaseGenericInlineFormSet(BaseModelFormSet):
294 294
     ct_field_name = "content_type"
295 295
     ct_fk_field_name = "object_id"
296 296
 
297  
-    def __init__(self, data=None, files=None, instance=None, save_as_new=None, prefix=None):
  297
+    def __init__(self, data=None, files=None, instance=None, save_as_new=None,
  298
+                 prefix=None, queryset=None):
  299
+        # Avoid a circular import.
  300
+        from django.contrib.contenttypes.models import ContentType
298 301
         opts = self.model._meta
299 302
         self.instance = instance
300 303
         self.rel_name = '-'.join((
301 304
             opts.app_label, opts.object_name.lower(),
302 305
             self.ct_field.name, self.ct_fk_field.name,
303 306
         ))
  307
+        if self.instance is None or self.instance.pk is None:
  308
+            qs = self.model._default_manager.none()
  309
+        else:
  310
+            if queryset is None:
  311
+                queryset = self.model._default_manager
  312
+            qs = queryset.filter(**{
  313
+                self.ct_field.name: ContentType.objects.get_for_model(self.instance),
  314
+                self.ct_fk_field.name: self.instance.pk,
  315
+            })
304 316
         super(BaseGenericInlineFormSet, self).__init__(
305  
-            queryset=self.get_queryset(), data=data, files=files,
  317
+            queryset=qs, data=data, files=files,
306 318
             prefix=prefix
307 319
         )
308 320
 
@@ -314,19 +326,6 @@ def get_default_prefix(cls):
314 326
         ))
315 327
     get_default_prefix = classmethod(get_default_prefix)
316 328
 
317  
-    def get_queryset(self):
318  
-        # Avoid a circular import.
319  
-        from django.contrib.contenttypes.models import ContentType
320  
-        if self.instance is None or self.instance.pk is None:
321  
-            return self.model._default_manager.none()
322  
-        qs = self.model._default_manager.filter(**{
323  
-            self.ct_field.name: ContentType.objects.get_for_model(self.instance),
324  
-            self.ct_fk_field.name: self.instance.pk,
325  
-        })
326  
-        if not qs.ordered:
327  
-            qs = qs.order_by(self.model._meta.pk.name)
328  
-        return qs
329  
-
330 329
     def save_new(self, form, commit=True):
331 330
         # Avoid a circular import.
332 331
         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 de64970

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