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')