Django select a valid choice error in form when using custom form template

630 Views Asked by At

I've been lurking for an answer for quite some time now, but I haven't found solution for my problems. I made custom template for my form and now, when I try to submit form I get this under choice field: Select a valid choice. is not one of the available choices.

I believe that problems is because I am not passing instance of organization but id. I tried {{ form.instance.organization }} and then I get None where should be choice field

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)

template:

<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>
0

There are 0 best solutions below