How do I save an object under two different classes using the admin page

27 views
Skip to first unread message

Alejandro Greppi

unread,
Sep 3, 2014, 2:36:42 PM9/3/14
to django...@googlegroups.com
I have an application that has defined these models:

class Manufacturer(models.Model):
    manufacturer = models.CharField(max_length=40, unique=True)
    def __str__(self):
        return self.manufacturer

and

class DeviceModel(models.Model):
    model = models.CharField(max_length=20, unique=True)
    manufacturer = models.ForeignKey(Manufacturer, to_field='manufacturer')
    def __str__(self):
        return self.model

I'm using django-treebeard, so I would like to add any newly added Manufacturer or DeviceModel into the tree hierarchy defined by:
class DeviceList(MP_Node):
    name = models.CharField(max_length=30)

    node_order_by = ['name']

    def __unicode__(self):
        return 'DeviceList: %s' % self.name

I was thinking that a good place to do that is in the admin page. When adding the manufacturer, it should save the new object Manufacturer into the table Manufacturer, and also create a new entry in the DeviceList table.
How do I do this two-fold save using the admin page?

Alex Chiaranda

unread,
Sep 4, 2014, 9:13:37 AM9/4/14
to django...@googlegroups.com
Hi,

in your admin.py you can implement your own version for the save_model function.


Regards,
Reply all
Reply to author
Forward
0 new messages