Sorry, I meant one ForeignKey to the Device model of openwisp-controller 
and one to the Ip model of openwisp-ipam.
You may as well create an admin inline and add it to the DeviceAdmin 
dynamically (it's called monkey patching) so you'll be able to see this in 
the django admin.

Here's an example of how to listen for signals in a django project and 
openwisp.

from django.apps import AppConfig
from django.db.models.signals import post_save

class ExampleApp(AppConfig):
    name = 'openwisp2.example_app'
    label = 'example_app'


    def ready(self):
        from openwisp_controller.config.models import Device


        post_save.connect(self.device_saved_receiver,
                          sender=Device,
                          dispatch_uid='device_saved_receiver')


    @classmethod
    def device_saved_receiver(cls, sender, instance, created, **kwargs):
        # if not created:
        #     False
        # do something
        pass

    @classmethod
    def device_deleted_receiver(cls, sender, instance, **kwargs):
        # may want to do something on delete
        pass


See https://docs.djangoproject.com/en/3.0/ref/applications/.
There's different way of doing this depending on how you want to do it.
If you do create a custom django app mixing openwisp-controller and 
openwisp-ipam you may upload it on github and we'll help you out there as 
well.

Alternatively, you can look for a simpler solution, in that case, you just 
need to place that file in the openwisp project folder (eg 
/opt/openwisp2/openwisp2 when using ansible-openwisp2) and then add to the 
INSTALLED_APPS the following: "openwisp2.example_app.ExampleApp".

Federico


On Thursday, February 6, 2020 at 2:37:46 PM UTC+1, João Henrique 
Albuquerque wrote:
>
> Just for clarifcation did you mean two ForeignKey pointing to the Device ?
> Shouldn't one be pointing to device config or device context?
>
> Creating a new django app in which you add a new model, eg: DeviceIp, in 
>> which you add a ForeignKey field pointing to Device and a ForeignKey 
>> pointing to Device, the two fields should be flagged as unique_together 
>> (look all these terms on the django documentation).
>>
>> Then add a signal receiver function in this new app which listens to save 
>> events on Device, return if the "created" parameter is False so that you 
>> act only on created devices, and in this function you add the logic to 
>> write to the context field of the Device. There should be a method in 
>> django-ipam to get the first available ip from a subnet.
>>
>> Then you should decide also how to manage subnets, if all devices are on 
>> the same subnet is easy, otherwise you should probably have a way to tell 
>> the system to which subnet the device pertains to. 
>>
>> Thanks
>> Federico
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"OpenWISP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to openwisp+unsubscr...@googlegroups.com.
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/openwisp/80494464-8f8c-407a-aaa8-9f837c350658%40googlegroups.com.

Reply via email to