I am simply trying to add a Bundle model to my Instrument model. It should be really easy but I can't seem to figure out why it's not working.
context.models.py
class Instrument(models.Model):
title = models.CharField(max_length=200)
bundle = models.ManyToManyField(Bundle, blank=True)
build.models.py
class Bundle(models.Model):
title = models.CharField(max_length=200)
views.py
def instrument_host_detail(request, pk_bundle, pk_instrument_host):
form_choose_instruments = ChooseInstrumentsForm()
form_choose_targets = ChooseTargetsForm()
bundle = Bundle.objects.get(pk=pk_bundle)
context_dict = {}
if request.method == 'POST':
form_choose_instruments = ChooseInstrumentsForm(request.POST)
form_choose_targets = ChooseTargetsForm(request.POST)
if form_choose_instruments.is_valid() and form_choose_targets.is_valid():
for instrument_name in form_choose_instruments.cleaned_data['instrument_list']:
instrument = Instrument.objects.get(title=instrument_name)
print bundle
print '{} -- {}'.format(instrument.pk, instrument)
instrument.bundle.add(bundle)
#instrument.save()
print instrument.bundle
context_dict['bundle'] = bundle
context_dict['form_choose_instruments'] = form_choose_instruments
context_dict['form_choose_targets'] = form_choose_targets
return render(request, 'detail.html', context_dict) When my view hits print bundle it will print the bundle title. When my view hits print
instrument.pk and instrument it will show it is an instrument model. However, if I try to add the bundle to my instrument model, I don't get any action. The print statement returns build.Bundle.None (where build_a_bundle is the app that Bundle model lives in). Also in the shell and admin I'm seeing that my bundle model isn't being added.
Note: The comment on instrument.save() was me toggling whether or not it was necessary and in both cases I'm not getting anything. The documentation didn't show that it was necessary.