can i add fields like address,city,state,country,pincode and security question and answer to the extension of UserCreationForm that currently contains username,email,password1 and password2. if yes then please illustrate how?
forms.py
class UserCreationForm(UserCreationForm):
email = EmailField(label=_("Email address"), required=True,
help_text=_("Required."))
city= forms.CharField(label= _("City"),max_length=20, required=True)
state= forms.CharField(label= _("State"),max_length=20, required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2","city","state")
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
user.city = self.cleaned_data["city"]
user.state = self.cleaned_data["state"]
if commit:
user.save()
return user
Yes just do like you did with email:
class UserCreationForm:
a_field = WhateverField(whatever='whatever'...)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
The field is now added in your form.
currently you are extending UserCreationForm class if you have other fields in user model then you can use forms.ModelForm and just mention other fields.
for example
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['username','name', 'email', 'phone',
'address','city','state', 'first_time']
if you want to use custom form then.
class UserForm(forms.Form):
name = forms.CharField(label= _("Name"))
address = forms.CharField(label=_("Address"))
etc.
Related
How would I grab the 2nd form and add it to the first form then selectively not allow that user to login. In the Doctorwizard done function.
maybe add a variable status?
etc.
username,password,email,first_name,last_name,verified
views.py
from django.core.files.storage import FileSystemStorage
import os
from django.conf import settings
class DoctorWizard(SessionWizardView):
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'doctor'))
template_name = "registration/signup.html"
form_list = [SignUpForm,verify]
def done(self, form_list, **kwargs):
data=process_data(form_list)
return redirect('home')
forms.py
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
class Meta:
model = Profile
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )
class verify(forms.Form):
verified = forms.ImageField(required=True)
class Meta:
model = Profile
fields = ('verified',)
models.py
class Profile(AbstractUser):
bio = models.TextField(max_length=100, blank=True)
phone_number = PhoneNumberField(max_length=25, region="US")
birth_date = models.DateField(blank = True, null = True)
is_doctor = models.BooleanField(default=False)
verified = models.ImageField(upload_to='media/doctor')
date_created = models.DateTimeField(auto_now_add=True)
avatar = models.ImageField(default='default.png', upload_to='')
def done(self, form_list, **kwargs):
process_data(form_list)
userCreate = form_list[0]
userCreate.save()
username = userCreate.cleaned_data.get('username')
raw_password = userCreate.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
if user:
user.verified=form_list[1].cleaned_data.get('verified')
user.is_doctor=True
user.is_active=False
user.save()
Just grab the user and access it's fields.
I have two tables of users and I am creating a form to store the user information.
Models.py
class MyUser(User):
address = models.CharField(max_length=200)
city = models.CharField(max_length=200)
expire_date = models.DateField()
This creates a table with user_ptr_id to the auth_user table of django.
I created two serializers: one for the User:
class UserSerializer(serializers.ModelSerializer):
first_name = serializers.CharField(min_length=2, required=True)
last_name = serializers.CharField(min_length=2, required=True)
email = serializers.EmailField(min_length=5, required=True)
password = serializers.CharField(min_length=8, write_only=True, required=True)
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'password')
def create(self, validated_data):
return UserSerializer(**validated_data)
And MyUser class:
class MyUserSerializer(serializers.ModelSerializer):
address = serializers.CharField(max_length=200)
city = serializers.CharField(max_length=200)
class Meta:
model = MyUser
fields = ('city', 'address')
def create(self, validated_data):
return MyUser(**validated_data)
As I am using Django Rest-Framework-Auth, I craeted a serializer to catch the data, but I don't know how to let the things work together. In the "MyUserSerializer" class, I also perform many validate checks, that I omitted here to keep the code clean.
Code below doesn't work
class UserSignup(serializers.Serializer):
user = UserSerializer()
my_user = MyUserSerializer()
confirm_psw = serializers.CharField(min_length=8, write_only=True, required=True)
def validate(self, data):
if not data["user"].get('password') or not data.get('confirm_psw'):
raise serializers.ValidationError("Please enter a password and confirm it.")
if data["user"].get('password') != data.get('confirm_psw'):
raise serializers.ValidationError("Those passwords don't match.")
return data
def save(self, validated_data):
user_data = self.validated_data["user"]
my_user = self.validated_data["my_user"]
return user_data, my_user
def create(self, validated_data):
user_data = validated_data.pop('user')
user = User.objects.create(user_data)
my_user = validated_data.pop('my_user')
my_user["user_ptr_id"] = user.id
MyUser.objects.create(**ua_user)
return user
How do I merge the two serializers to correctly perform the user registration?
Thanks
I'm using this code (in forms.py) for my custom signup form for allauth:
class RegistrationForm(UserCreationForm):
birth_date = forms.DateField(widget=extras.SelectDateWidget(years=BIRTH_DATE_YEARS))
class Meta:
model = get_user_model()
fields = ('username', 'email', 'password1', 'password2', 'first_name', 'last_name', 'gender', 'birth_date', 'city', 'country')
I have of course specified ACCOUNT_SIGNUP_FORM_CLASS in settings.py to point to this form and it displays the fields which I've put in it. However, it does not work on submission, even if I put signup method it never gets invoked. I've tried with the save but still it's the same - it raises an error that one of my custom added fields is blank while creating user and saving it to the database.
So, what's the correct code for achieving this?
I've made it thanks to #rakwen who has found the correct solution here. I wrote my custom adapter and put it in adapters.py in my app:
from allauth.account.adapter import DefaultAccountAdapter
class AccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=False):
data = form.cleaned_data
user.username = data['username']
user.email = data['email']
user.first_name = data['first_name']
user.last_name = data['last_name']
user.gender = data['gender']
user.birth_date = data['birth_date']
user.city = data['city']
user.country = data['country']
if 'password1' in data:
user.set_password(data['password1'])
else:
user.set_unusable_password()
self.populate_username(request, user)
if commit:
user.save()
return user
Then I've specified ACCOUNT_ADAPTER in settings.py to point to that adapter, and it finally started to work!
In your project setting.py try to add this line:
verify if you have added this:
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
ACCOUNT_SIGNUP_FORM_CLASS = "yourapp.forms.customSignupForm"
In app.models
class CustomModel(models.Model):
"""CustomModel docstring."""
city = forms.CharField(max_length=75)
country = forms.CharField(max_length=25)
other....
In app.forms:
class SignForm(forms.ModelForm):
"""SignForm docstring."""
username = forms.CharField(
max_length=30,
)
first_name = forms.CharField(
max_length=30,
)
last_name = forms.CharField(
max_length=30,
)
field...
def myclean():
def signup(self, request, user):
"""You signup function."""
# dont forget to save your model
class Meta:
model = mymodel.CustomModel
fields = [
'city',
'country',
'field...',
]
Try this method work !
Had implemented a basic authentication system using function based views in django.
Trying to upgrade it to class based views.
Creating a UserProfile by inheriting from a django User model.
Need to serialize UserProfile and return to client side
User model :
from django.contrib.auth.models import User
UserProfile model :
class UserProfile(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User)
profile_picture = models.ImageField(upload_to='documents', blank=True)
def __str__(self):
return self.user.username
UserSerializer:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username','password', 'first_name', 'last_name', 'email',)
write_only_fields = ('password',)
read_only_fields = ('is_staff', 'is_superuser', 'is_active', 'date_joined',)
def restore_object(self, attrs, instance=None):
user = super(UserSerializer, self).restore_object(attrs, instance)
user.set_password(attrs['password'])
return user
UserProfileSerializer:
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = UserProfile
fields = ('id','user','profile_picture',)
views.py:
class AuthView(APIView):
authentication_classes = (BasicAuthentication,)
def post(self, request, *args, **kwargs):
login(request, request.user)
content={ 'user':UserProfileSerializer(request.user).data,'token':csrf.get_token(request)}
return Response(content)
UserProfileSerializer(request.user).data in views.py is not working.
but instead if i use:
UserSerializer(request.user).data, it gives me result(as expected) :
{'first_name': '', 'username': 'admin', 'email': 'a#a.com', 'last_name': '', 'password': 'pbkdf2_'}
But i also want additional attributes to the user also serialized like profile_picture, hence something like
UserProfileSerializer(request.user).data
should work for me.
Questions:
Is it possible to serialize a model containing FileField ?
How to serialize a nested object and return its data ?
Kinda beginner here.
yes it is possible to serialize a FileField. The problem is that your profile serializer needs a UserProfile model and not a User model. Try this:
content={ 'user':UserProfileSerializer(request.user.user_profile).data,'token':csrf.get_token(request)}
Hey in django admin there is a button "adduser". When I press it I want to show my custom Form. I tried to do the following.
at admin.py
add_form = CustomUserCreationForm
add_fieldsets = (
(None, {'fields': ('username', 'email', 'first_name', 'last_name', 'password1', 'password2',), }),
)
P.S. I think the names interfered with the standard names. Once I renamed the fields from first_name to something custom it kidna started working.
at forms.py
class CustomUserCreationForm(UserCreationForm):
first_name = forms.CharField(max_length=35, label='First name')
last_name = forms.CharField(max_length=35, label='Last name')
email = forms.EmailField(label="Email")
class Meta:
model = User
fields = ('first_name', 'last_name', 'username', 'email',)
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.first_name = first_name
user.last_name = last_name
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
The only charfield that is editable that appears on teh admin-page is the "username". Others just have a label but not an edit-field which is odd. I would like to display all the CharFields I defined in the CustomForm and them to be editable. Thats how my AddUser looks like:
You need to extend the built-in UserCreationForm from django.contrib.auth.forms instead of extending ModelForm.
This way you don't have to override the save method to set the password, however you'll need to add the username, password1 and password2 fields to your form, or it won't pass validation.