def sensortoroom(request, room_id):
if request.method == "POST":
form = Addsensortoroom(request.POST)
if form.is_valid():
sensor = request.POST.get("sensors")
print(sensor)
room = Rooms.objects.get(id=room_id)
room.sensors_set.add(Sensors.objects.get(name=sensor))
room.save()
else:
form = Addsensortoroom()
return render(request, 'room/addsensortoroom.html', {'form': form, 'room_id': room_id})
class Addsensortoroom(forms.Form):
sensors = Sensors.objects.all()
sens = forms.ChoiceField(sensors)