#2248 closed enhancement (wontfix)
[patch] inline_models, replacement for edit_inline and core=True
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | minor | Keywords: | edit_inline |
Cc: | tyson@…, jkocherhans@…, gary.wilson@…, karsten@…, ruckc@… | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I always found the edit_inline syntax quite strange. So, I decided to try something different: let the inline models be configured in the model itself, not in the children models. I came up with the given patch, which basically allows the use of inline models the following way:
class Person(models.Model): name = models.CharField(maxlength=20) class Admin: inline_models = ( {'model':'Child', 'type':models.TABULAR, 'min_num_in_admin':3, 'max_num_in_admin':20, 'num_extra_on_change':3, 'fields':('name','age',) }, {'model':'Job', 'type':models.STACKED, 'min_num_in_admin':1, 'max_num_in_admin':3, 'fields':('company','salary',) } ) class Child(models.Model): parent = models.ForeignKey(Person) name = models.CharField(maxlength=100) age = models.IntegerField() hair_color = models.CharField(maxlength=20,blank=True,null=True) class Job(models.Model): person = models.ForeignKey(Person) company = models.CharField(maxlength=100) salary = models.IntegerField() section = models.CharField(maxlength=50)
Attachments (3)
Change History (19)
by , 18 years ago
Attachment: | inline_models_in_Admin.diff added |
---|
comment:2 by , 18 years ago
Summary: | Replacement for edit_inline and core=True → [patch] inline_models, replacement for edit_inline and core=True |
---|
by , 18 years ago
Attachment: | inline_models_in_Admin.2.diff added |
---|
[patch] a *much* better patch for inline_models
by , 18 years ago
Attachment: | inline_models_in_Admin.3.diff added |
---|
Third patch, 'fields' isn't a good name for the core fields list, updated to 'core_fields'
comment:5 by , 18 years ago
Cc: | added |
---|
comment:6 by , 18 years ago
Is it possible to add something related to the the ordering of multiple inline related tables? Using vanilla 0.95 I have 3 tables related to one model currently and there doesn't seem to be a reasoning to how they're pulled in (not alphabetical by table name or not the same as the order in the models.py file). Maybe simply the order you list them in the inline_models list would be enough to satisfy this.
I like this patch. I think it consolidates the features of the admin and cleans up the model code.
comment:7 by , 18 years ago
Cc: | added |
---|
comment:8 by , 18 years ago
Cc: | added |
---|
comment:9 by , 18 years ago
Cc: | added |
---|
comment:11 by , 18 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
Adrian has an alternative suggestion here: http://groups.google.com/group/django-developers/browse_thread/thread/704546f4b30d5c86/a4ead31d11b8a79a
that uses objects:
from django.contrib.admin import TabularInline, StackedInline # ... class Admin: inlines = ( TabularInline('Child', min=3, max=20, extra=3, fields=('name', 'age')), StackedInline('Job', min=1, max=3, fields=('company', 'salary')), )
comment:12 by , 18 years ago
I'm not sure if this is the best place for this, but another things that would be nice about inlines is to have an option to require at least one to be created. For example:
class Admin:
inline_models = (
{'model':'Child',
'type':models.TABULAR,
'min_num_in_admin':3,
'max_num_in_admin':20,
'num_extra_on_change':3,
'num_required': 1,
'fields':('name','age',)
},
{'model':'Job',
'type':models.STACKED,
'min_num_in_admin':1,
'max_num_in_admin':3,
'fields':('company','salary',)
}
)
would require at least one Child to be created.
comment:13 by , 18 years ago
Adrian's idea is definitely better than mine, as it's cleaner and more extensible. The original idea is much easier to implement, though.
comment:14 by , 18 years ago
I've been looking at the problem you're trying to solve and I must say that while your patch would really solve it.
On the other hand, it feels like a hack. From the OO point of view, it means defining how the base class is going to be used in each child class inside the base class itself! I think all the options should be in the child class. I'm not familiar with Django's internals but it would be much more natural to have all this machinery defined in the foreign key. Someone (sorry, don't have a ref) suggested adding an "edit_inline_here" keyword in the foreign key, which would do the same thing.
While this might sound like nitpicking for the current example, it means to worlds when you're trying to design a modular app. If you have a Person class somewhere in your app and you want to extend it you should be able to do so without modifying the base class.
comment:15 by , 17 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
This implementation was discarded in favor of the TabularInline
and related classes in newforms-admin.
comment:16 by , 17 years ago
Keywords: | edit_inline added |
---|
patch for the new inline_models syntax