How to correctly write a condition in view Django?

The meaning of the program is to select analogues from the list and link them. The problem is that it binds all values regardless of the selected ones.
I think the problem is in the wrong if. How to fix it

My view:

def editpart(request, id, **kwargs):

    if request.method == 'POST':
        part.name = request.POST.get("name")
        part.description = request.POST.get("description")

        analogs = Part.objects.all()
        for analog_zap in analogs:
            analog = analog_zap.analog
                if Part.objects.filter(analog_analog = analog):
                        part.analog.add(analog_zap.id)

My model:

class Part(models.Model):
    name = models.CharField('Название', max_length=100)
    analog = models.ManyToManyField('self', blank=True, related_name='AnalogParts')

not sure what you’re trying to do with this–do you only want to do the “add” if there’s at least a Part object that satisfies the condition in filter?

In that case, you’re looking for: if Part.objects.filter(analog_analog = analog).exists()

The problem is that it binds all values regardless of the selected ones

part.name = request.POST.get("name")
part.description = request.POST.get("description")

Where is part coming from? You’re just assigning to its attributes but there is no mention of where it’s defined

part = Part.objects.get(id=id)