[Answered ]-Add ManyToMany in Django

1👍

✅

You have to get the model instance and then just add it to your ‘new_rdp’. You could do something like this:

@login_required(login_url='login')
def add_rdp(request):
        if request.method == "POST":

                get_rdp = (request.POST.get('rdp', ''))
                get_number = (request.POST.get('contract_number',''))

                for new_contract in contract.objects.all():
                        if new_contract.number == get_number:
                                new_rdp = rdp(config = get_rdp, contract = new_contract).save()

        return render(request, 'add_rdp.html')

1👍

Because You calling save() when no contract attached.

You can change definition of model:

class rdp(models.Model):
        contract = models.ForeignKey(contract, blank = True)
        config = models.TextField()

Leave a comment