Hi Arthur,

sorry for my delayed response.

Here's the code to add a template to a device.

device = Device.objects.get(id='<device-id-here>')
template = Template.objects.get(id='<template-id-here>')
device.config.templates.add(template)

You can also add more templates in the same call.

device.config.templates.add(t1, t2, t3)

if you want to loop over a list of devices and set the templates for each 
of them you can do something like:

t1 = Template.objects.get(id='<id1>')
t2 = Template.objects.get(id='<id2>')
devices = Device.objects.all()  # you could use filter() to query for a 
subset of devices


for device in devices:
    device.config.templates.add(t1, t2)

Refer to the django ORM (object relational mapper) documentation for more 
information:

https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/

The base templates of openwisp-controller are listed 
here: 
https://github.com/openwisp/django-netjsonconfig/tree/master/django_netjsonconfig/base

template.py, device.py and config.py are the relevant ones.

I hope this helps.
Federico


On Monday, October 8, 2018 at 12:46:55 PM UTC+2, BlancLoup wrote:
>
> Hello.
> We need to link template with specified devices.
> Our code create new template and receive list of devices id. How i 
> undestand all m2m links stored at "config_config_templates". And it's quite 
> enough to add record for each config id like 
> "config_id, temlate_id".
> Example of code:
>
> *    # template creation*
>
>
> *    template.save()    self.template = template.id <http://template.id>*
> *    for device in json.loads(str(self.devices_list).replace('\'', '\"')):*
>
> *        config_id=Config.objects.get(device_id=device)*
>
> *        # link device and template. What object to use if there is no 
> model for config-template?*
> *        ConfigTemplates.objects.create(config_id=config_id, 
> template_id=self.template)*
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to