I am trying to extends django user auth through onetonefield in model. Here is the model:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
nim = models.CharField(max_length=10, blank=True)
def __str__(self):
return self.user.username
With basic admin panel i get to create user instance first and then profile. But i use custom forms on admin panel so i can create it on one form.This is the forms look:
this is forms.py
from django import forms
from django.contrib.auth.models import User
from .models import Profile
class ProfileForm(forms.ModelForm):
username = forms.CharField(required=False, max_length=150)
password = forms.CharField(required=False, max_length=50, widget=forms.PasswordInput)
class Meta:
model = Profile
fields = ('username', 'password', 'nim')
and this is admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from .forms import ProfileForm
from .models import Profile
class ProfileAdmin(admin.ModelAdmin):
form = ProfileForm
def save_model(self, request, obj, form, change):
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = User.objects.create_user(username=username, password=password)
user.save()
obj.user = user
obj.save()
admin.site.register(Profile, ProfileAdmin)
With this all i can create profile instance just fine. But when i try to change/edit profile it didn't show initial value for username and password fields, only nim field did.
empty username and password field
Thanks for your help