Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#2248 closed enhancement (wontfix)

[patch] inline_models, replacement for edit_inline and core=True

Reported by: joaoma@… Owned by: nobody
Component: contrib.admin Version: master
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: UI/UX:

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)

inline_models_in_Admin.diff (5.6 KB) - added by joaoma@… 10 years ago.
patch for the new inline_models syntax
inline_models_in_Admin.2.diff (5.0 KB) - added by JoaoJoao 10 years ago.
[patch] a *much* better patch for inline_models
inline_models_in_Admin.3.diff (5.0 KB) - added by JoaoJoao 10 years ago.
Third patch, 'fields' isn't a good name for the core fields list, updated to 'core_fields'

Download all attachments as: .zip

Change History (19)

Changed 10 years ago by joaoma@…

Attachment: inline_models_in_Admin.diff added

patch for the new inline_models syntax

comment:1 Changed 10 years ago by Adrian Holovaty

Oooooh! I haven't looked at the patch, but the idea is fantastic.

comment:2 Changed 10 years ago by anonymous

Summary: Replacement for edit_inline and core=True[patch] inline_models, replacement for edit_inline and core=True

comment:3 Changed 10 years ago by JoaoJoao

I'm trying a decent patch, the one I attached is ugly.

Changed 10 years ago by JoaoJoao

[patch] a *much* better patch for inline_models

Changed 10 years ago by JoaoJoao

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

comment:4 Changed 10 years ago by Tyson Tate <tyson@…>

Cc: tyson@… added

I second this. I'd really like to see this implemented.

comment:5 Changed 10 years ago by anonymous

Cc: jkocherhans@… added

comment:6 Changed 10 years ago by treborhudson@…

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 Changed 10 years ago by Gary Wilson <gary.wilson@…>

Cc: gary.wilson@… added

comment:8 Changed 10 years ago by Karsten W. Rohrbach <karsten@…>

Cc: karsten@… added

comment:9 Changed 10 years ago by ruckc@…

Cc: ruckc@… added

comment:10 Changed 10 years ago by (none)

milestone: Version 1.0

Milestone Version 1.0 deleted

comment:11 Changed 10 years ago by Gary Wilson <gary.wilson@…>

Needs tests: set
Triage Stage: UnreviewedDesign 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 Changed 10 years ago by sansmojo@…

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 Changed 10 years ago by joaoma@…

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 Changed 10 years ago by philippe.raoult@…

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 Changed 9 years ago by James Bennett

Resolution: wontfix
Status: newclosed

This implementation was discarded in favor of the TabularInline and related classes in newforms-admin.

comment:16 Changed 9 years ago by Antonis Christofides <anthony@…>

Keywords: edit_inline added
Note: See TracTickets for help on using tickets.
Back to Top