Python Forum
Django: Adding Row Data To Existing Model Instance Question/Problem.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django: Adding Row Data To Existing Model Instance Question/Problem.
#1
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

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},)
Reply
#2
from django.db import models

class MyModel1(models.Model):
field_1 = models.CharField(max_length=200)


class MyModel2(models.Model):
name = models.CharField(max_length=200)
model_1 = models.ManyToManyField(MyModel1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  model.fit and model.predict errors hatflyer 6 1,341 Nov-10-2023, 01:39 AM
Last Post: hatflyer
  Python 3.11 data import question love0715 2 819 Mar-05-2023, 06:50 PM
Last Post: snippsat
  Django authenticate problem Carolzi 0 657 Oct-24-2022, 07:10 AM
Last Post: Carolzi
  problem adding two numpy arrays djf123 2 2,104 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  magic related field in Django model sonh 1 1,244 Apr-24-2022, 12:37 PM
Last Post: sonh
  What happens line by line after I register a model in django Novichok 1 1,238 Apr-03-2022, 03:05 PM
Last Post: sastonrobert
  Adding shifted data set to data set xquad 3 1,516 Dec-22-2021, 10:20 AM
Last Post: Larz60+
Big Grin question about simple algorithm to my problem jamie_01 1 1,691 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  FileNotFoundError: [Errno 2] No such file or directory: 'model/doc2vec.model/Articles Anldra12 10 5,831 Jun-11-2021, 04:48 PM
Last Post: snippsat
Bug Djongo (mongoDb) - Django rest framework Problem with search on JsonFields grzybekrafal 0 2,479 May-11-2021, 09:04 AM
Last Post: grzybekrafal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020