neo4j-contrib / django-neomodel

Neomodel plugin for Django
MIT License
287 stars 55 forks source link

[Admin] implement choices with Django framework #31

Open vignesk70 opened 5 years ago

vignesk70 commented 5 years ago

I simply want to have a dropdown with country and save its code to neo4j as a string property. I am able to generate the drop down but it doesn't match what I want to achieve.

models.py

COUNTRY_CODE1=[("AF", 'Afghanistan'), ('BL', 'Albania'), ('DZ', 'Algeria')]

class Person(DjangoNode):
    uid_person = UniqueIdProperty()
    ind_name = StringProperty(max_length=100 , label='Enter your First Name',required=True)
    ind_last_name = StringProperty(max_length=100,null=True, label='Last Name',required=True)

ind_nationality = StringProperty(max_length=2,choices=COUNTRY_CODE1,label='Nationality',required=True) 
    class Meta:
        app_label = 'reg'

forms.py

    class Meta:
        model=Person
        fields = ['ind_name','ind_last_name', 'ind_nationality']#,'ind_country']#,'ind_address1','ind_address2','ind_address3', 'ind_town','ind_postcode','ind_state', 'ind_birthdate','ind_gender','ind_email','ind_countryiddcode','ind_contactnum']
        widgets = {

            'ind_name' : forms.TextInput(attrs={'size':50}),
            'ind_last_name' : forms.TextInput(attrs={'size':50}),
            'ind_nationality' : forms.Select(),

        }
        app_label = 'reg'

views.py

class PersonRegView(generic.FormView):
    template_name='reg/personreg.html'
    form_class=PersonRegForm
    success_url = 'index'

    def post(self, request, *args, **kwargs):

        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        print('view post',form)

        if (form.is_valid()): 
            return self.form_valid(form)
        else:
            #print(x)
            return self.form_invalid(form)

    def form_valid(self, form):
        self.object = form.save()
        #return HttpResponseRedirect(self.get_success_url())
        return HttpResponseRedirect(self.request.path_info)

    def form_invalid(self, form):

        return self.render_to_response(
            self.get_context_data(form=form)
            )

html template - using bootstrap4.

<div class="row justify-content-center">
        <div class="media-container-column col-lg-8" > <!-- data-form-type="formoid"-->
                <div data-form-alert="" hidden="">
                    {%bootstrap_form_errors form%}
                </div>

                <form class="mbr-form" action="#" method="post" data-form-title="PersonRegForm">

                  {% csrf_token %}

                  <div class="row row-sm-offset">
                    <div class = "col-sm-12"><h3> Personal Information </h3></div>

                      <div class="col-md-6 multi-horizontal" data-for="name">
                          {%bootstrap_field form.ind_name show_label=False%}
                      </div>
                     <div class="col-md-6 multi-horizontal" data-for="name">
                          {%bootstrap_field form.ind_last_name show_label=False%}
                      </div>
                      <div class="col-md-6 multi-horizontal" data-for="name">
                           {%bootstrap_field form.ind_nationality show_label=True %}
                       </div>

                  </div>

                    <span class="input-group-btn">
                        <button type="submit" class="btn btn-primary btn-form display-4">SEND FORM</button>
                    </span>
                </form>
        </div>

    </div>

My output shows that for the choices appears like this.

<select name="ind_nationality" class="form-control is-invalid" title="" required="" id="id_ind_nationality">
  <option value="">---------</option>

  <option value="A">F</option>

  <option value="B">L</option>

  <option value="D" selected="">Z</option>

</select>

I have two problems here . First one being the dropdown is incomplete I am assuming it should display

<option value="AF">Afghanistan</option>

Second problem is that when I try to save it throws an error invalid choice with whatever option selected.

I am able to successfully save it without dropdown choices just the first two fields.

anuj9196 commented 1 year ago

@vignesk70 Were you able to solve this issue? I'm also facing the same issue.