0

I have extended my user's table with profile table, but while registration of user site only ask email and password for user type "customer" and for user type "promoter" there is another registration form which requires other fields also like date of birth, address, country, state, city etc etc. But after login if we go to account settings page , the account setting page of both types of users are same, that's why for account setting i have created a single django form.

  class userAccountSettingsForm(forms.Form):
      email = forms.EmailField(required = True, max_length=50, widget=forms.EmailInput(attrs={
          'class' : 'custome-input promote-input', 
          'autocomplete' : 'off',
          'data-empty-message' : 'Please enter a email.',
          'data-error-message' : 'Please enter a valid email.',
          'readonly' : 'readonly'
      }))
      firstname = forms.CharField(required = True, max_length=50, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input', 
          'autocomplete' : 'off',
          'data-empty-message':'Please enter first name.' 
      }))
      lastname = forms.CharField(required = True, max_length=50, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input', 
          'autocomplete' : 'off',
          'data-empty-message':'Please enter last name.' 
      }))
      dob = forms.DateField(required = False, widget=forms.DateInput(attrs={
          'class' : 'custome-input promote-input',
          'id' : 'custom-datepicker',
          'autocomplete' : 'off',
          'readonly':'readonly'
      }))
      phone = forms.IntegerField(required = True, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input',
          'autocomplete' : 'off',
          'data-empty-message':'Please enter mobile number.'
      }))
      address = forms.CharField(required = False, max_length=225, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input',
          'autocomplete' : 'off'
      }))
      subscription = forms.BooleanField(required = False)

The thing is that while registering promoter it also creates its row in profile table and while registering customer it only creates row in users table, so thats why in account settings page i have to find out the user's data from model, i am doing this with the help of django form in view, here is my code of view :-

dobForm=""
if my_details.profile.dob:
    dobForm = datetime.strptime(str(my_details.profile.dob), '%Y-%m-%d').strftime('%m/%d/%Y')
settingsForm = userAccountSettingsForm(
    initial={
        'email': my_details.email,
        'firstname': my_details.first_name,
        'lastname': my_details.last_name,
        'dob': dobForm,
        'phone':my_details.profile.phone_number,
        'address':my_details.profile.address,
        'subscription':my_details.profile.subscription,
        }
    )

So using this page it fetches user's all existing data, in case of user type promoter it is not creating any issue because there is already a row generated for promoter in profile table while registration but in case of customer it does not generate row for customer in profile table while registration, and in this case it give error :-

RelatedObjectDoesNotExist at /home/account-settings

    User has no profile

Apart from also creating a row in profile table while customer's registration, is there any other solution for this issue ?

1 Answers1

0

OneToOneField will raise this exception if object does not exist. So you just have to catch that exception.

Other solution may be to use hasattr, as suggested by Sagar in the comments, but you should be aware of some potential issues if you use python < 3.2.

Community
  • 1
  • 1