views:

278

answers:

1

Hello, I want to extend the admin panel of Django where adding/changing happens for a specific object, say Foo. I have already done alot of research and found a part discussing this at the old version djangobook.

I have made an admin/myapp/foo/change_list.html at my template directory and did some experient such as adding some html and it works well.

And this is where problem starts, I need to tweak the view of this admin display(no overriding the whole thing but just extending it) so that, with a small GET/POST form at the change_list.html, I want to do a specific action such as:

Say I have 2 objects Foo and Bar:

class Bar(models.Model):
  bar_text = models.TextField( )


class Foo(models.Model):
  name = models.TextField( )
  my_bar = models.models.ForeignKey( Bar)

The regular admin interface while adding a new Foo, I can pick a single my_bar foreignKey as it should be, now I want a tickbox at the end of the page "add to all" which makes the same input(name for my example) connected for all Bar objects. So after save I will have several Foo objects which are having same "name" field and each has a different Bar connection

A: 

if you know how to extend the admin (i assume that from your post), all you need is to override the save() method of the form, so it updates all the foos if the checkbox is checked.

def save(self):
    if (self.cleaned_data['save_to_all']):
        foos = Foo.objects.all()
        for obj in foos:
             obj.name = self.cleaned_data['name']
             obj.save()
    super(FooForm, self).save()

did i understand well what you need or i missed sth?

zalew