How to make required=false in Django registration custom fields for particular case

Usman Maqbool

I have override the Django Registration app and added some custom fields in signup form.

I have added feature that on selection of particular value, required field will hide. I am successful in it now I want to remove required for my particular field.

I have tried below and many other ways but on selection of value from dropdown, required field hide but on click signup button I get

enter image description here

my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script>
      $('#id_type').change(function(){

        var type = $(this).val();
        if(type === "dealer"){
            $('#id_dealership_name').show();
            $("label[for='id_dealership_name']").show();
        }
        else{
            $("#id_dealership_name").removeAttr("required")
            $('#id_dealership_name').hide();
            $("label[for='id_dealership_name']").hide();
        }
      });

  </script>

EDIT 1

form.py

class DealerForm(forms.ModelForm):
    class Meta:
        model = Dealer
        exclude = ('user', 'site')

    def __init__(self, *args, **kwargs):
        super(DealerForm, self).__init__(*args, **kwargs)
        self.fields['dealership_name'].required = False

EDIT 2

views.py

class DealerRegistrationView(RegistrationView):
    def __init__(self, **kwargs):
        super(DealerRegistrationView, self).__init__(**kwargs)
        RegistrationForm.base_fields.update(DealerForm.base_fields)

    form_class = RegistrationForm

    def register(self, form):
        new_user = super(DealerRegistrationView, self).register(form)

        if Site._meta.installed:
            site = Site.objects.get_current()
        if form.cleaned_data['type'] == "dealer":
            new_dealer = Dealer(
                user=new_user,
                site_id=site.id,
                whatsapp=form.cleaned_data['username'],
                dealership_name=form.cleaned_data['dealership_name'],
            ).save()
Usman Maqbool

I resolved it by making changes in forms.py and then applied JavaScript and it worked for me.

from django import forms
from registration.forms import RegistrationForm, RegistrationFormUniqueEmail


class DealerForm(RegistrationFormUniqueEmail):
    type = forms.ChoiceField(required=True, choices=(
        ('dealer', 'Dealer'),
        ('marketing_agent', 'Marketing Agent'),
        ('private_seller', 'Private Seller'),
        ('virtual_dealer', 'Virtual Dealer')
    ))
    dealership_name = forms.CharField(required=False, max_length=50)
    subdomain_name = forms.CharField(required=False, max_length=50)
    banner_image = forms.ImageField(required=False)
    logo_image = forms.ImageField(required=False)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django: Make certain fields in a ModelForm required=False

From Dev

How can I make all fields in Django admin not required by default?

From Dev

django-rest-framework how to make model serializer fields required

From Dev

How do I make User fields required in Django Rest Framework?

From Dev

Adding custom user registration fields in django

From Dev

How to make a Django custom management command argument not required?

From Dev

How to make django registration form

From Dev

Django Forms: Make Two Fields XOR Required?

From Dev

Django Forms: Make Two Fields XOR Required?

From Dev

How to add custom-fields (First and Last Name) in django-registration 2.2 (HMAC activation Workflow)?

From Dev

form.isValid() always return false in custom registration form django

From Dev

How to make fields appear required (by colouring)

From Dev

Is it required to make a custom manger for a custom user model in django

From Dev

Django custom registration form

From Dev

JsonSchemaGenerator Not Setting Fields to required = false

From Dev

How can I make JRadioButton transparent in particular case?

From Dev

Django AuthenticationForm required fields

From Dev

Django AuthenticationForm required fields

From Dev

Django Faking Required Fields

From Dev

How to make Django DateTimefield non required?

From Dev

Django 1.7: how to make ManyToManyField required?

From Dev

FluentValidation: how to make bool as required field with 'false' as valid input?

From Dev

Does anyone know how to make Required fields work on Safari 5

From Dev

how to make javascript alert not run until all required fields are filled

From Dev

How to make a custom Eclipse run launcher that launches a particular Class?

From Dev

Django-registration, custom registration and validation

From Dev

Django Registration Redux custom registration form

From Dev

Django-registration, custom registration and validation

From Dev

why I get three password fields when I define my custom registration form in Django?

Related Related

  1. 1

    Django: Make certain fields in a ModelForm required=False

  2. 2

    How can I make all fields in Django admin not required by default?

  3. 3

    django-rest-framework how to make model serializer fields required

  4. 4

    How do I make User fields required in Django Rest Framework?

  5. 5

    Adding custom user registration fields in django

  6. 6

    How to make a Django custom management command argument not required?

  7. 7

    How to make django registration form

  8. 8

    Django Forms: Make Two Fields XOR Required?

  9. 9

    Django Forms: Make Two Fields XOR Required?

  10. 10

    How to add custom-fields (First and Last Name) in django-registration 2.2 (HMAC activation Workflow)?

  11. 11

    form.isValid() always return false in custom registration form django

  12. 12

    How to make fields appear required (by colouring)

  13. 13

    Is it required to make a custom manger for a custom user model in django

  14. 14

    Django custom registration form

  15. 15

    JsonSchemaGenerator Not Setting Fields to required = false

  16. 16

    How can I make JRadioButton transparent in particular case?

  17. 17

    Django AuthenticationForm required fields

  18. 18

    Django AuthenticationForm required fields

  19. 19

    Django Faking Required Fields

  20. 20

    How to make Django DateTimefield non required?

  21. 21

    Django 1.7: how to make ManyToManyField required?

  22. 22

    FluentValidation: how to make bool as required field with 'false' as valid input?

  23. 23

    Does anyone know how to make Required fields work on Safari 5

  24. 24

    how to make javascript alert not run until all required fields are filled

  25. 25

    How to make a custom Eclipse run launcher that launches a particular Class?

  26. 26

    Django-registration, custom registration and validation

  27. 27

    Django Registration Redux custom registration form

  28. 28

    Django-registration, custom registration and validation

  29. 29

    why I get three password fields when I define my custom registration form in Django?

HotTag

Archive