Jun-04-2022, 12:25 PM
Hello. So my problem is quite simple and I suspect the answer is also quite simple. I'm doing a project in Django and I've a Database with three models:
Beacon: Which collects data about hosts that send data to it.
Command_Node: Linked to beacon, which collects via a form commands to be executed by the host in question.
Executed_Commands_Node: Linked to beacon, which hasn't been fully implemented yet, but will collect any Commands contained within a Command_Node and put them within that Command_Nodes_Node, when they are quered/collected by a connecting machine via a function to be implemented, it will also delete the Commands in a Command_Node as they have now been acted upon. (To be implemented)
Problems
What I want to do is add multiple commands to a single instance of a Command_Node. Prefereably via the form I have already, which allows me to select a Command_Node and select what command to give it. However at present there are two problems I have in doing this, the first of these is that when I submit the form, it creates a new instance of a Command_Node rather then edit the selected one. The second problem is that I don't know if my present setup will allow me to insert multiple Commands into a single instance of a Command_Node, which I imagine in the DB would be represented by just more rows of the Current_Command column.
Any ideas on how I could do that, would be greatly appreciated. As would be really helpful to me and help me to learn more. Thanks but no one got back to me.
models.py
Beacon: Which collects data about hosts that send data to it.
Command_Node: Linked to beacon, which collects via a form commands to be executed by the host in question.
Executed_Commands_Node: Linked to beacon, which hasn't been fully implemented yet, but will collect any Commands contained within a Command_Node and put them within that Command_Nodes_Node, when they are quered/collected by a connecting machine via a function to be implemented, it will also delete the Commands in a Command_Node as they have now been acted upon. (To be implemented)
Problems
What I want to do is add multiple commands to a single instance of a Command_Node. Prefereably via the form I have already, which allows me to select a Command_Node and select what command to give it. However at present there are two problems I have in doing this, the first of these is that when I submit the form, it creates a new instance of a Command_Node rather then edit the selected one. The second problem is that I don't know if my present setup will allow me to insert multiple Commands into a single instance of a Command_Node, which I imagine in the DB would be represented by just more rows of the Current_Command column.
Any ideas on how I could do that, would be greatly appreciated. As would be really helpful to me and help me to learn more. Thanks but no one got back to me.
models.py
from tkinter import CASCADE from turtle import update from django.db import models class beacon(models.Model): host_id = models.BigAutoField('Id', primary_key=True) hostname = models.CharField('Hostname', max_length=200) internalIp = models.CharField('Internal-IP', max_length=200) externalIp = models.CharField('External-IP', max_length=200) current_user = models.CharField('Current_User', max_length=200) os = models.CharField('OS', max_length=200) admin = models.CharField('Admin', max_length=200) def __str__(self): return self.hostname CHOICES = [ ('Sleep', "Sleep"), ('Open SSH_Tunnel', 'Open SSH_Tunnel'), ('Close SSH_Tunnel', 'Close SSH_Tunnel'), ('Open TCP_Tunnel', 'Open TCP_Tunnel'), ('Close TCP_Tunnel', 'Close TCP_Tunnel'), ('Open Dynamic', 'Open Dynamic'), ('Close Dynamic', 'Close Dynamic'), ('Task', 'Task'), ] class command_node(models.Model): host_id = models.ForeignKey(beacon, on_delete=models.CASCADE) current_commands = models.CharField(choices=CHOICES, max_length=50, null=True) def __str__(self): return str(self.host_id) class Executed_Command_Node(models.Model): host_id = models.ForeignKey(beacon, on_delete=models.CASCADE) Executed_Commands = models.CharField('Executed_Commands', max_length=2000, null=True)Forms.py
from django import forms from django.forms import ChoiceField, ModelForm, RadioSelect from .models import command_node from .models import beacon CHOICES = [ ('Sleep', "Sleep"), ('Open SSH_Tunnel', 'Open SSH_Tunnel'), ('Close SSH_Tunnel', 'Close SSH_Tunnel'), ('Open TCP_Tunnel', 'Open TCP_Tunnel'), ('Close TCP_Tunnel', 'Close TCP_Tunnel'), ('Open Dynamic', 'Open Dynamic'), ('Close Dynamic', 'Close Dynamic'), ('Task', 'Task'), ] class Command_Form(ModelForm): class Meta: model = command_node fields = ( 'host_id', 'current_commands' ) host_id = forms.ModelChoiceField( required=True, queryset=beacon.objects.all(), widget=forms.SelectMultiple( attrs={ 'class': 'form-control' }, ) ) current_comamnds = forms.ChoiceField( required=True, choices=CHOICES )Views.py
from django.shortcuts import render from django.http import HttpResponse from .models import beacon from .models import command_node from .forms import Command_Form from django.http import HttpResponseRedirect def home(request): form = Command_Form() if request.method == "POST": form = Command_Form(request.POST) if form.is_valid(): form.save() return render(request, 'home.html', {"form": form}) return render(request, 'home.html', {"form": form},)