0

I am using django-registration app. I've two registration form A and B. A has default registration fields. And in B I've added custom fields and a signal. Following signal also execute for both registration form. That's why I added if form.is_valid(). and it sucks. I want to execute only when BForm is called. Is there any alternative? Can anyone help me to improve this code? thanks

class BForm(RegistrationForm):
    """
    Subclass of ``RegistrationForm``
    """
    phone = forms.CharField(max_length=100, required=False)

Signal

def user_created(sender, user, request, **kwargs):
    form = BForm(request.POST)
    if form.is_valid():
        .....
user_registered.connect(user_created)

2 Answers2

0

As per Django's documentation on the topic, you can simply do this:

user_registered.connect(user_created, sender=BForm)

and it should work.

Gonzalo
  • 4,145
  • 2
  • 29
  • 27
0

On submission of both the forms, the signal will be executed. This happens because signal is sent from the register view of django-registration after a user is registered. So, no matter what form is used, if a user is successfully registered, signal user_registered will be executed.

Regarding your issue with form.is_valid():

Since you have required=False on phone field of BForm, even if phone is not present in POST data, your BForm will be valid and the code in your reciever function user_created would be executed.

You can make following changes to user_created to make sure that only when BForm is used then execute the reciever function.

def user_created(sender, user, request, **kwargs):
    if 'phone' in request.POST:
        .....
        .....
user_registered.connect(user_created)
Community
  • 1
  • 1
Akshar Raaj
  • 14,231
  • 7
  • 51
  • 45