Ticket #2248: inline_models_in_Admin.3.diff

File inline_models_in_Admin.3.diff, 5.0 KB (added by JoaoJoao, 9 years ago)

Third patch, 'fields' isn't a good name for the core fields list, updated to 'core_fields'

Line 
1--- django/db/models/base.py    2006-06-27 15:08:19.000000000 -0300
2+++ django/db/models/base.py    2006-06-27 15:35:35.000000000 -0300
3@@ -150,6 +150,45 @@
4 
5         if hasattr(cls, 'get_absolute_url'):
6             cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
7+       
8+        # Retrieves the list of fields with related objects
9+        # (Foreign keys)
10+        fk_fields = [x for x in cls._meta.fields if x.rel]
11+        thiscls_fqn = "%s.%s" % (cls.__module__,cls.__name__)
12+        for field in fk_fields:
13+            # Retrieve model that is related to the field
14+            rel_model = field.rel.to
15+            # Check if the related model as a inline_models field
16+            if not hasattr(rel_model,'_meta'):
17+                continue
18+            if rel_model._meta.admin:
19+                for inline_info in rel_model._meta.admin.inline_models:
20+                    model_fqn = inline_info['model']
21+                    if not '.' in model_fqn:
22+                        model_fqn = "%s.%s" % (cls.__module__,model_fqn)
23+                    # There this model is defined in the related model's
24+                    # inline_models entry
25+                    if model_fqn == thiscls_fqn:
26+                        # Set edit_inline and other attributes in the model
27+                        # According to the inline_models entry
28+                        rel_fieldname = inline_info.get('related_field',None)
29+                        # The field name can be set with the 'related_field'
30+                        # argument
31+                        if rel_fieldname:
32+                            rel_field = cls._meta.get_field(rel_fieldname)
33+                        else:
34+                            fields = [x for x in cls._meta.fields if x.rel]
35+                            rel_field = [x for x in fields if  x.rel.to==rel_model][0]
36+                        for key,val in inline_info.iteritems():
37+                            if key=='type':
38+                                rel_field.rel.edit_inline = val
39+                            elif key=='core_fields':
40+                                for field_name in val:
41+                                    cls._meta.get_field(field_name).core = True
42+                            elif hasattr(rel_field.rel,key):
43+                                setattr(rel_field.rel,key,val)
44+                       
45 
46         dispatcher.send(signal=signals.class_prepared, sender=cls)
47diff -ur --exclude-from=exc django_src/django/db/models/options.py django_src_new/django/db/models/options.py
48--- django/db/models/options.py 2006-06-27 13:31:39.000000000 -0300
49+++ django/db/models/options.py 2006-06-27 10:25:18.000000000 -0300
50@@ -201,7 +201,8 @@
51 class AdminOptions(object):
52     def __init__(self, fields=None, js=None, list_display=None, list_filter=None,
53         date_hierarchy=None, save_as=False, ordering=None, search_fields=None,
54-        save_on_top=False, list_select_related=False, manager=None, list_per_page=100):
55+        save_on_top=False, list_select_related=False, manager=None, list_per_page=100,
56+        inline_models=[]):
57         self.fields = fields
58         self.js = js or []
59         self.list_display = list_display or ['__str__']
60@@ -212,6 +213,7 @@
61         self.save_on_top = save_on_top
62         self.list_select_related = list_select_related
63         self.list_per_page = list_per_page
64+        self.inline_models = inline_models
65         self.manager = manager or Manager()
66 
67     def get_field_sets(self, opts):
68diff -ru --exclude-from=exc django_src_new/docs/model-api.txt django_src/docs/model-api.txt
69--- django/docs/model-api.txt   2006-06-27 10:16:10.000000000 -0300
70+++ django/docs/model-api.txt   2006-06-27 14:00:53.000000000 -0300
71@@ -1176,6 +1176,34 @@
72 under the heading of the fieldset. It's used verbatim, so you can use any HTML
73 and you must escape any special HTML characters (such as ampersands) yourself.
74 
75+``inline_models``
76+-----------------
77+A list of dicts that define which related models should be edited inline with
78+the given model. It's a cleaner alternative to edit_inline, with the
79+following differences:
80+  - You define the model with the 'model' keyword argument. You have to use
81+    the model as a ``string``, not the ``class``.
82+  - You can use the 'related_field' keyword to define the related model's
83+    ForeignKey that will be used, or leave Django to catch the first
84+    ForeignKey field to the parent model.
85+  - Instead of the 'edit_inline' keyword, you use the 'type' keyword.
86+  - Instead of specifying core=True in each field you want to show in
87+    the inline admin, you use the 'core_fields' keyword argument, which is
88+    a list of fields
89+All other options can be used, e.g. ``min_num_in_admin``.
90+
91+Example::
92+    inline_models = (
93+        {'model':'InlineModel',
94+         'type':models.TABULAR,
95+         'min_num_in_admin':1,
96+         'num_extra_on_change':1,
97+         'core_fields':('field1','field2','field3')
98+        },
99+    )
100+
101+Check the ``edit_inline`` section for more arguments on this option.
102+
103 ``js``
104 ------
105 
Back to Top