title: Resolving A Django Error on Form: Object has no attribute
body: <p>Hi I'm trying to create a form that will when used update one model (Command_Node), and at the same time create an instance of another model (EC_NODE) that has a many to one relationship with the Command_Nodes.</p>
  <p>However when I go onto the update view and submit the form I'm getting the following error any ideas on how I can resolve this error?</p>
  <p>Thanks for any help you can offer.</p>
  <pre><code>AttributeError at /website/update/1
  'Beacon' object has no attribute 'EC_Node_set'
  Request Method: POST
  Request URL:    http://127.0.0.1:8000/website/update/1
  Django Version: 4.0.4
  Exception Type: AttributeError
  Exception Value:    
  'Beacon' object has no attribute 'EC_Node_set'
  </code></pre>
  <p>This on traceback points to</p>
  <pre><code>       command_form.EC_Node_set.all()  # &lt;- not sure with all _ and Maj here 
  </code></pre>
  <p>Which I can understand. I think my intention here should be clear enough. I want to set an instance of EC_Node to hold the command just put in via the form, and I understand the error. I just don't know how to get around it so that the view/form does what I want.</p>
  <p><strong>Relevant views.py</strong></p>
  <pre><code>def update(request, host_id):
      host_id = Command_Node.objects.get(pk=host_id)
      form = Command_Form(request.POST or None, instance=host_id)
      if form.is_valid():
          
          # Original suggestion was command_form = Command_Form.objects.first()
          command_form = form.cleaned_data['host_id']
          command_form.EC_Node_set.all()  # &lt;- not sure with all _ and Maj here
          
          form.save()
          return redirect('home')
      return render (request, 'update.html', {'host_id':host_id,'form':form})
  </code></pre>
  <p><strong>Relevant models.py</strong></p>
  <pre><code>class Command_Node(models.Model):
      host_id = models.ForeignKey(Beacon, on_delete=models.CASCADE)
      current_commands = models.CharField(choices=CHOICES, max_length=50, null=True)
      
      def __str__(self):
          return str(self.host_id)
  
  class EC_Node(models.Model):
      Command_node = models.ForeignKey(Command_Node, on_delete=models.DO_NOTHING)
      command = models.CharField(choices=CHOICES, max_length=50, null=True)
  
      def __str__(self):
          return str(self.Command_node)
  </code></pre>
  <p><strong>Relevant forms.py</strong></p>
  <pre><code>class Command_Form(ModelForm):
      class Meta:
          model = Command_Node
          fields = ('host_id','current_commands')
  
          host_id = forms.ModelChoiceField(
              required=True,
              queryset=Beacon.objects.all(),
              widget=forms.SelectMultiple(
                  attrs={
                      'class': 'form-control'
                  },
              )
          )
  
          current_comamnds = forms.ChoiceField(
              required=True,
              choices=CHOICES
          )
  
      def save(self, **kwargs):
          EC_Node.objects.create(
          command=self.cleaned_data[&quot;current_commands&quot;],
          Command_node=self.instance
          )
          return super().save(**kwargs)
  </code></pre>