Deleting from a existing database that uses formset

How do I use the key that I am passing from page A to B to access the database and delete. For models, it will be models.objects.get(variable=key), but how about for data that are stored using formset. I tried the following but it does not work.

di_formset = modelformset_factory(DeviceInterface, fields=(‘moduletype’, ‘firstportid’, ‘lastportid’), can_delete=True)
deviceInterface = di_formset.objects.get( I2DKEY=pk)

You don’t store data using a formset. A formset is a UI tool. Data is stored in models.

You delete data from Models. In the general case, a model populated from a formset is typically related to some other model with a ForeignKey. So if you want to delete those related items, you need to identify those items related to the base model being deleted. (Of course, if you have on_delete=models.CASCADE defined in that related model, the ORM will remove those instances automatically.)

It might be clearer if you post the actual models involved. Then we can talk about this in terms of the models and fields involved.

I tried deleting it using the way i delete for my other models. It does not work. Which gives me the impression that formset requires some different way to delete. Below is my models for my web

models.py

  class Device(models.Model):
    hostname = models.CharField(max_length=50)
    ipaddr = models.GenericIPAddressField(protocol='ipv4')
    date_added = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.hostname
class DeviceDetail(models.Model):
    
    SUBNET_CHOICES = (
    ('16','16'),
    ('17', '17'),
    ('18','18'),
    ('19','19'),
    ('20','20'),
    ('21', '21'),
    ('22', '22'),
    ('23', '23'),
    ('24', '24'),
    ('25', '25'),
    ('26', '26'),
    ('27', '27'),
    ('28', '28'),
    ('29', '29'),
    ('30', '30'),
    )

    DEV_MODS =(
        ('Catalyst 9606R', 'Catalyst 9606R'),
        ('C9300L-48T-4X', 'C9300L-48T-4X')
    )

    hostname = models.CharField(max_length=50)
    mgt_interface = models.CharField(max_length=50)
    mgt_ip_addr = models.GenericIPAddressField(protocol='ipv4')
    subnetmask = models.CharField(max_length=2, choices = SUBNET_CHOICES)
    ssh_id = models.CharField(max_length=50)
    ssh_pwd = models.CharField(max_length=50)
    enable_secret = models.CharField(max_length=50)
    dev_mod=models.CharField(max_length=50, choices = DEV_MODS) ##device_model replacement
    DD2DKEY = models.ForeignKey(Device, on_delete=models.CASCADE) ##The key to link up the tables
    
    def __str__(self):
        return self.hostname

class DeviceInterface(models.Model):
    MODULE_ID_CHOICES = (
        ('TenGigabitEthernet','TenGigabitEthernet'), 
        ('FortyGigabitEthernet','FortyGigabitEthernet'),
        ('GigabitEthernet','GigabitEthernet'),
        ('Ethernet','Ethernet'),        
    )
    
    moduletype = models.CharField(max_length = 50,choices = MODULE_ID_CHOICES)
    firstportid = models.CharField(max_length=50)
    lastportid = models.CharField(max_length=50)
    I2DKEY = models.ForeignKey(Device, on_delete=models.CASCADE) ##The key to link up the tables

    def __str__(self):
        return self.moduletype

Here is my views in case u need it to guide where i go wrong. Before deviceInterface was changed to formset, it was just using a normal model. And all i had to do was pass my pk to the foreign key so it can access the certain row of the database and delete it. But now it’s in formset and this don’t seem to work

   def device_delete(request, pk):
     device = Device.objects.get(pk=pk)
     deviceDetail = DeviceDetail.objects.get(DD2DKEY=pk)
     di_formset = modelformset_factory(DeviceInterface, fields=('moduletype', 'firstportid', 'lastportid'), can_delete=True)
     deviceInterface = di_formset(I2DKEY=pk)

     if request.method == 'POST':
         device.delete()
         return redirect('/device/', {'device':Device.objects.all, 'devices':device})

     return render(request, 'interface/device_delete.html', {'devices':device, 'deviceDetail':deviceDetail, 'deviceInterface':deviceInterface})

If these are your models, you don’t need to do anything else.

When any Device instance is deleted, the on_delete=models.CASCADE will delete all related instances of DeviceInterface.

And no, the formsets have nothing to do with this. They’re strictly a UI tool, they play no part in the deletion of objects.

Oh wait, sorry about that. Was not thinking properly. But just for knowledge wise, if i want to access the data or update the models’s database, how do i do it? PS: For DeviceInterface, i know how to do it for Device & DeviceDetail as they are using the model without the UI help. But for DeviceInterface, I error which said it return 4 items instead of 1. How do i deal with this?

The Model.objects.get method returns one object. The Model.objects.filter method returns a queryset.

Ok thanks. But how about displaying wise. Because now it returns me queryset but it does not display. How do i separate this queryset and display it one by one?

How do you display any list of items in a template?

By looping them. This I know but how do i get certain items. Because I want to display them in individual textboxes. I dont know much about django honestly. Just started out so im not sure how formset label them because if u see from my models, each items from the queryset have a few variables.

I am looping thru them. For form in di_form. But if i {{form.moduleid}} or other variables, it doesnt appear. Is there certain labels for them that formset assign to them?

I managed to display the details out. But theres 2 issue. I cant display the moduletype in a dropdown box nor can i use the jquery method i used for the other fields and 2nd, the div are all having the identical IDs. Is there any advise for this?