Using django 1.9
In our project we decided to use e-mail field instead of username field of User.
We tried to do it without overriding django.contrib.auth's User model, so just hid the username field in registration form to fill it with e-mail field's data. The problem is the username field has max_len=30, e-mail field - max_len=254.
Which is the best approach to solve it without overriding User model, if possible?
Related
I have seen some posts like Making form fields - read only or disabled in DJANGO updateView use custom form on an UpdateView to make some fields as read only fields. However, the form object is not accessible for request.user object. If I want to make some fields read-only only for some user group, is there way to do it under UpdateView?
I am using djnago all-auth to create custom user accounts. When creating an account with email and password, if account with a email already exits it gives an error (UNIQUE constraint failed: account_emailaddress.email) but I would like to display message that an account with this email already exists instead of throwing an error. What is the best way to handle this? In general I would use AJAX to verify and display message for my own views but I do not know how to deal here with django all-auth package.
I'll suggest that you should override the signup/login form in order to manage this error. Have you checked the documentation? https://django-allauth.readthedocs.io/en/latest/forms.html
I think this answer is related to your question.
A relatively similar approach is given in this answer:
Create your custom view that inherits SignupView and overrides the form class
Create a custom form that inherits from SignupForm and overrides the email validation message
In your own urls.py add the following after include('allauth.urls') to override the account_signup url
Since djangoallauth take care of unique constrain you don't have to add unique=True to your field if user try to login with any social media account with email id already present in your database it djangoallauth will simple ignore and will not set email id in your user model. :)
I am handling my unique fields i.e Email field manually
I have a custom User model with email as username.
I have encrypted the email field to be in conformity with GPDR (I will hold a lot of personal information).
I have added a email_hash field with index on it for the database to be able to retrieve immediately user.
I have modified get_natural_key of my user object manager to use the hash for retrieve.
But now i face a problem I have to disable the uniqueness on field email (username field) but Django don't let me do it when i try to makemigrations.
myuser.MyUser: (auth.E003) 'MyUser.email' must be unique because it is named as the 'USERNAME_FIELD'.
Otherwise, I want the uniqueness error to be fired on email field and not on email_hash field ....
How to have functional encrypted email field as user and stored hash for index ?
edit:
I have disable uniqueness check on email field and added SILENCED_SYSTEM_CHECKS = ["auth.E003"] in settings.
Now my problem is to have uniqueness error of email_hash rendered as email error to have "A user with that email address already exists." message displayed on correct forms and django rest framework serializer field.
I only use Linked-In as means to authenticate. When the user gives permission in Linked-In, he get's send to my own form so I can gather extra information. But it seems Allauth only lets me add fields to the default form using:
ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.MySignUpForm'
But the email field is always visible (and filled in with the email from Linked-In). Is it true that I have no way of dropping this field? I don't want the user to be able to change his email into something else than his Linked-In email.
I'm using django.contrib.auth.views.login to log in users. When login is failed, the user and password fields get posted back to the form.
What's the proper way to clean those?
Pass render_value=False when you create your password field's PasswordInput widget.
class YourAuthenticationForm(AuthenticationForm):
password = forms.CharField(widget=forms.PasswordInput(render_value=False))