- dir()
- autocompletion - po ewaluacji
- root
- Po Primary Key
Device.objects.get(pk=1)
- Po typach
from ralph.discovery.models import DeviceType
Device.objects.filter(model__type=DeviceType.rack.id)
- Wszystkie
Device.objects.all()
- Po adresie ip
import ipaddr
Device.objects.filter(ipaddress=int(ipaddr.IPAddress('127.0.0.1')))
-
warunek "AND"
Po prostu kolejne argumenty do filter po przecinkach lub &
-
warunek "OR"
from django.db.models import Q
Device.objects.filter(Q(model__name__contains='VMware') | Q(pk__gt=1))
- Zbiory (sets)
d = Device.objects.all()[0]
d.ethernet
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'Device' object has no attribute 'ethernet'
d.ethernet_set.all()
[<Ethernet: eth0: Broadcom Corporation NetXtreme II BCM5709 Gigabit Ethernet (18A9053BBBCC)>, <Ethernet: .... ]
for d in Device.objects.filter(venture__name='Cloud'):
print d.verified
- Prosta zmiana 1 obiektu
d = Device.objects.get(pk=1)
d.name = '...'
d.save()
- Zmiana wiele obiektów naraz
Network.objects.filter(pk__gt=1000).update(max_ip=0)
- Dodawanie wartości do "listy" https://ralph.office/admin/discovery/network/625/
Network.objects.get(pk=625).racks.all()
[<Device: DC4::Rack 312 (1948487)>]
moj_rack = ...
n = Network.objects.all()[0]
n.racks.add(moj_rack)
https://github.com/allegro/ralph
- Deploymenty w trakcie
from ralph.deployment.models import DeploymentStatus
Deployment.objects.filter(status=DeploymentStatus.in_progress).all()
- Lista deployment id / mac
[(x.id, x.mac) for x in Deployment.objects.all()]
ls - /home/ralph/project-allegro/scripts
from ralph.discovery.models_device import Device
import csv
csvfile=open('/tmp/output.csv', 'wb')
writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
x = [ (d.venture, d.role,d.name, d.id, d.cached_price, d.processor_set.count(), d.storage_set.count(), d.memory_set.count() ) \
for d in Device.objects.filter(venture__department__name='ZUIS2') \
if not(d.cached_price) or (not d.processor_set.count() or not d.memory_set.count() or not d.storage_set.count()) ]
for row in x:
writer.writerow(row)
csvfile.close()
Gdzie szukac informacji o metodach/funkcjach dostepnych do operowania na obiektach ralpha z shella? Czego absolutnie nie można robic i na co szczególnie uwazac żeby nie popsuc ralpha?
# Nie używaj delete()
device.deleted = True
device.save()
# Uważaj na update() - lepiej iterować