Home > other >  how to display first_name in database on django
how to display first_name in database on django

Time:11-25

`views.py

from allauth.account.views import SignupView
from .forms import HODSignUpForm

class HodSignUp(SignupView):

template_name = 'account/signup.html'
form_class = HODSignUpForm
redirect_field_name = ''
view_name = 'hod_sign_up'

def get_context_data(self, **kwargs):
ret = super(HodSignUp, self).get_context_data(**kwargs)
ret.update(self.kwargs)
return ret

forms.py

from .models import Admin
from po.models import User
from allauth.account.forms import SignupForm

class HODSignUpForm(SignupForm):
first_name=forms.CharField(required=False)
last_name=forms.CharField(required=False)

class Meta:
model= Admin
fields = ['first_name','last_name']

def save(self,request):
user = super(HODSignUpForm, self).save(request)
user.is_hod = True
user= User(first_name=self.cleaned_data.get('first_name'),
last_name=self.cleaned_data.get('last_name'))
user.save()
return user

models.py

from po.models import User
class Admin(models.Model):
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
first_name = models.CharField(max_length=30, db_column='first_name')
last_name = models.CharField(max_length=30, db_column='last_name')

po.models.py

from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
is_active = models.BooleanField(default=True)
is_hod= models.BooleanField(default=False)
first_name = models.CharField(null=True, max_length=50)
last_name=models.CharField(null=True, max_length=50)
admin.py

from po.models import User

class Schooladmin(admin.ModelAdmin): list_display = ("id","is_active","is_hod","first_name","last_name") list_filter = ("is_active","is_hod") add_fieldsets = ( ('Personal Info', { 'fields': ('first_name', 'last_name') }), )

admin.site.register(User,Schooladmin)

enter image description here i want this image show name but how does show firstname and lastname on database? enter image description here here's porblem.

CodePudding user response:

Add verbose_name when creating class:

from po.models import User
class Admin(models.Model):
    user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
    first_name = models.CharField(max_length=30, db_column='first_name', verbose_name='Name')
    last_name = models.CharField(max_length=30, db_column='last_name')

Or you can add class Meta to models and use labels:

labels = {
    'first_name': 'Name'
}

CodePudding user response:

Try this:

from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    list_display = ('is_active', 'is_hod', 'first_name', 'last_name')

and

from po.models import User
class Admin(models.Model):
     user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
     first_name = models.CharField(max_length=30, db_column='first_name', verbose_name='Name')
    last_name = models.CharField(max_length=30, db_column='last_name')
    is_active = models.BooleanField(default=True)
    is_hod= models.BooleanField(default=False)

CodePudding user response:

forms.py:

from .models import Admin
from po.models import User
from allauth.account.forms import SignupForm

class HODSignUpForm(SignupForm):

    class Meta(SignupForm.Meta):
        model= Admin
        fields = ['first_name','last_name']

        labels = {'first_name': 'Name'}

        def save(self,request):
            user = super(HODSignUpForm, self).save(request)
            user.is_hod = True
            user= User(first_name=self.cleaned_data.get('first_name'),
            last_name=self.cleaned_data.get('last_name'))
            user.save()
        return user
  • Related