group inlines in django admin

Posted by pablo on Stack Overflow See other posts from Stack Overflow or by pablo
Published on 2010-03-27T02:44:40Z Indexed on 2010/03/27 2:53 UTC
Read the original article Hit count: 330

Filed under:
|
|

Hi

I have a two models, Model1 and Model2. Model2 has a FK to Model1 and FK to iteself. In the admin I show Model2 as inlines in Model1 change_form.

I want to modify the way the inlines are shown in the admin. I need to group all the instances that have the same parent_model2 and display them as a readonly field with a string of 'childs' in the parent Model2 instance.

I know how to use itertools.groupby (or the django version) but don't know how to do it in the admin. What should I override to be able to iterate over all the Model2 instances, group them by parent, add children to the parent and remove children from the inlines?

class Model1(models.Model):
    name = models.CharField()

class Model2(models.Model):
    name = models.CharField()
    fk_model1 = models.ForeignKey('self', blank=True, null=True)
    parent_model2 = models.ForeignKey('self', blank=True, null=True)

Thanks

© Stack Overflow or respective owner

Related posts about django

Related posts about admin