1👍
There are several ways to extend the user model in Django and looks like you picked the wrong one for your use-case.
Try to extend the built-in user model using inheritance instead.
class UserWithProfile(AbstractBaseUser):
...
date_of_birth = models.DateField()
relationship_status = models.IntegerField(choices=RS)
height = models.FloatField()
...
REQUIRED_FIELDS = ['date_of_birth', 'height']
And at settings.py:
AUTH_USER_MODEL = 'myapp.UserWithProfile'
0👍
I found a much simpler method, where I was able to get this working by creating my own custom modelform:
class GirlModelForm(forms.ModelForm):
username = forms.CharField(
label=_('username'), max_length=30,
help_text=_(
'Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'
),
validators=[
validators.RegexValidator(
r'^[\w.@+-]+$', _('Enter a valid username.'), 'invalid'
)
])
first_name = forms.CharField(label=_('first name'), max_length=30)
last_name = forms.CharField(label=_('last name'), max_length=30)
email = forms.EmailField(label=_('email address'))
# The attribute required must be False because of the clean() workaround
user = forms.ModelChoiceField(
queryset=get_user_model().objects.all(), required=False
)
class Meta:
model = Girl
# fields = [
# 'degree', 'extra_field'
# ]
def __init__(self, *args, **kwargs):
if not kwargs.get('initial'):
kwargs['initial'] = {}
if kwargs.get('instance'):
kwargs['initial'].update({
'username': kwargs['instance'].user.username,
'first_name': kwargs['instance'].user.first_name,
'last_name': kwargs['instance'].user.last_name,
'email': kwargs['instance'].user.email,
})
super(GirlModelForm, self).__init__(*args, **kwargs)
def clean(self):
"""
This is a nasty workaround and it exists so that we can create/edit
the user directly in the Userprofiles. The motive is referenced here
http://stackoverflow.com/q/27235143/977622 and basically the idea is
to make the built in user NOT REQUIRED and GET or CREATE the user
object and create a new attribute in the cleaned_data with the user
object
"""
cleaned_data = super(GirlModelForm, self).clean()
user, created = get_user_model().objects.get_or_create(
email=cleaned_data['email'],
defaults={
'first_name': cleaned_data['first_name'],
'last_name': cleaned_data['last_name'],
'username': cleaned_data['username'],
}
)
if not created:
# If the user exists update it
user.first_name = cleaned_data['first_name']
user.last_name = cleaned_data['last_name']
user.username = cleaned_data['username']
user.save()
cleaned_data['user'] = user
Maybe there is a better way to do it but this is what i was able to do without redoing my models
- [Answer]-Why can't I objects.get() a Django User by pk or id?
- [Answer]-How to arrange fields of a form rendered by ModelForm?
Source:stackexchange.com