如何在django admin中访问我的模型?

时间:2019-11-16 20:08:29

标签: django-models django-forms

我正在django的AlumniTracker上工作,到目前为止,我已经创建了用于用户注册的表格和一些其他信息。 即使保存了其他信息表格,我也无法在django-admin中访问它。

我要在此处添加模型,视图和表单文件。

views.py

def student_profile(request):
    if request.method == 'POST':
        form = StudentDetailForm(request.POST)
        if form.is_valid:
            student_form = form.save(commit=False)
            student_form.user = request.user
            student_form.save()
            return redirect(reverse('homepage'))
    else:
        form = StudentDetailForm()

    return render(request, 'authentication/student_profile.html', {'form':form})


def alumni_profile(request):
    if request.method == 'POST':
        form = AlumniDetailForm(request.POST)
        if form.is_valid:
            alumni_form = form.save(commit=False)
            alumni_form.user = request.user
            alumni_form.save()
            return redirect(reverse('homepage'))
    else:
        form = AlumniDetailForm()

    return render(request, 'authentication/student_profile.html', {'form':form})

forms.py


class StudentDetailForm(ModelForm):
    class Meta:
        model = StudentDetail
        fields = ['first_name', 'last_name', 'contact_no', 'birth_date', 'course', 'session_start', 'session_end']


class AlumniDetailForm(ModelForm):
    class Meta:
        model = AlumniDetail
        exclude = ['user']

models.py


class CustomUser(AbstractUser):
    profile = models.CharField(max_length=10,choices=PROFILE_CHOICES, default='student')


    def __str__(self):
        return self.username


class StudentDetail(models.Model):
    first_name    = models.CharField(max_length=20)
    last_name     = models.CharField(max_length=20)
    contact_no    = models.IntegerField()
    birth_date    = models.DateField()
    course        = models.CharField(max_length=50)
    session_start = models.IntegerField()
    session_end   = models.IntegerField()
    user          = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)


class AlumniDetail(models.Model):
    first_name    = models.CharField(max_length=20)
    last_name     = models.CharField(max_length=20)
    contact_no    = models.IntegerField()
    birth_date    = models.DateField()
    course        = models.CharField(max_length=50)
    session_start = models.IntegerField()
    session_end   = models.IntegerField()
    company       = models.CharField(max_length=60)

1 个答案:

答案 0 :(得分:0)

打开并编辑admin.py 并通过在admin import下面添加代码来注册模型

from .models import CustomUser,StudentDetail,AlumniDetail

admin.site.register(CustomUser)
admin.site.register(StudentDetail)
admin.site.register(AlumniDetail)