Django select valid select error on form when using custom form template

I've been hiding behind the answer for a while now, but I haven't found a solution for my problems. I created a custom template for my form and now when I try to submit the form I get this in the select box: select the correct choice. is not one of the options available.

I believe the problems are due to the fact that I am not passing the organization instance, but the id. I tried {{form.instance.organization}} and then I got None where the select box should be

views.py:

class AddNewView(generic.View):
formClass = AddNewForm
template_name = 'myapp/subscription_form.html'

def get(self, request):
    groups = self.request.user.groups.values_list('id', flat=True).first()
    form = self.formClass(groups, None)
    return render(request, self.template_name, {'form': form})

def post(self, request):

    groups = self.request.user.groups.values_list('id', flat=True).first()
    form = self.formClass(groups, request.POST)

    if form.is_valid():
        subscription = form.save(commit=False)

        organization = request.user.groups.values_list('id', flat=True).first()
        input_user = self.request.user
        stuff = form.cleaned_data['stuff']
        description = form.cleaned_data['description']

        subscription.save()

    return render(request, self.template_name, {'form': form})

      

forms.py:

class AddNewForm(forms.ModelForm):
  def __init__(self, groups,*args,**kwargs):
      super (AddNewView, self ).__init__(*args,**kwargs) 
      self.fields['organization'].queryset = Organization.objects.filter(group=groups)

  class Meta:
      model = Subscription
      fields = [
          'organization',
          'stuff',
          'description',
      ]

      

models.py:

class Organization(models.Model):
  d_number = models.CharField(max_length=25)
  city = models.CharField(max_length=100)
  group = models.ManyToManyField(Group, help_text="groups")

class Subscription(models.Model):
  organization = models.ForeignKey(Group, help_text="which organization")
  input_user = models.CharField(max_length=150)
  input_date = models.DateTimeField(auto_now_add=True)
  description = models.CharField(max_length=1000, null=True, blank=True, help_text='Description')
  stuff = models.CharField(max_length=100)

      

:

<form action="" method="post">
    {% csrf_token %}
        <!-- Left Inputs -->
        <div class="col-xs-6 wow animated slideInLeft" data-wow-delay=".5s">
            <!-- Organization -->
            {{ form.non_field_errors }}
            <div class="fieldWrapper">
                {{ form.organization.errors }}
                <label>Organization:</label>
                {{ form.organization }}
            </div>

            <!-- stuff -->
            {{ form.non_field_errors }}
            <div class="fieldWrapper">
                {{ form.stuff.errors }}
                <input type="text" name="stuff" id="id_stuff" required placeholder="stuff" class="form"/>
            </div>

        </div><!-- End Left Inputs -->
        <!-- Right Inputs -->
        <div class="col-xs-6 wow animated slideInRight" data-wow-delay=".5s">
            <!-- description -->
            {{ form.non_field_errors }}
            <div class="fieldWrapper">
                <textarea name="description" id="id_description" class="form textarea"  placeholder="description"></textarea>
            </div>
        </div><!-- End Right Inputs -->
        <div class="relative fullwidth col-xs-12">
            <!-- Send Button -->
            <button type="submit" class="form-btn semibold">Vnesi</button>
        </div><!-- End Bottom Submit -->
</form>

      

+3


source to share





All Articles