ManyToManyField edited on admin by both sides?

145 views
Skip to first unread message

Markos Gogoulos

unread,
Sep 19, 2008, 8:09:57 AM9/19/08
to Django users
Hi there,

on the admin panel I am trying to display both models of an
ManyToManyField relationship. Say for example there are these two
models:

class Software(models.Model):
name = models.CharField(max_length=50)
categories = models.ManyToManyField(Category)

class Category(models.Model):
name = models.CharField(max_length=50)

Obviously when I edit a Software object within the admin interface,
categories will be displayed, as Software contains the actual
reference, and I can choose which categories this object belongs to.

On the other side, I ALSO want software to be displayed when I edit a
Category object, which is not possible at the moment.

I've seen about the Intermediary Models but I wouldn't like to use it,
because there has to be a simpler way for my problem!

I would also rather avoid replacing/overriding templates...


So one though is to pass the set to a Category object to be displayed.
I added get_software() on Category class's definition

def get_software(self):
return self.software_set.all()


and then on admin.py the fields = ['name', 'get_opens'] but this
didn't do anything, instead gives the exception


'CategoryAdmin.fields' refers to field 'get_opens' that is missing
from the form.

Any ideas/suggestions?

Thanks in advance anyone looking this post!
Regards

Jerry Stratton

unread,
Sep 19, 2008, 9:45:02 AM9/19/08
to Django users
On Sep 19, 5:09 am, Markos Gogoulos <mgogou...@gmail.com> wrote:
> on the admin panel I am trying to display both models of an
> ManyToManyField relationship. Say for example there are these two
> models:
>
> class Software(models.Model):
>     name = models.CharField(max_length=50)
>     categories = models.ManyToManyField(Category)
>
> class Category(models.Model):
>     name = models.CharField(max_length=50)

Assuming you mean two-way inlines, I discovered yesterday that it's
very easy to do this in Django 1.0 using the model's admin.py and the
new inlines system:

class SoftwareInline(admin.StackedInline):
model = Software

class CategoryAdmin(admin.ModelAdmin):
inlines = [SoftwareInline]

Besides StackedInline you also have TabularInline.

Jerry

Jerry Stratton

unread,
Sep 19, 2008, 9:48:44 AM9/19/08
to Django users
On Sep 19, 6:45 am, Jerry Stratton <goo...@hoboes.com> wrote:
> class SoftwareInline(admin.StackedInline):
>         model = Software
>
> class CategoryAdmin(admin.ModelAdmin):
>         inlines = [SoftwareInline]

Edit: shouldn't post before caffeine. The above might work, but I
don't know if it does; I used it yesterday with a ForeignKey, not a
ManyToManyField.

Jerry

Markos Gogoulos

unread,
Sep 19, 2008, 10:39:05 AM9/19/08
to Django users
Hi Jerry

this doen't work with ManyToManyField

Exception
<class '...'> has no ForeignKey to <class '...'>

Regards

Markos Gogoulos

unread,
Oct 4, 2008, 3:37:34 PM10/4/08
to Django users
Reply all
Reply to author
Forward
0 new messages