简体   繁体   English

在 Django 中使用更多字段扩展用户 model 的最佳方法是什么?

[英]What is the best way to extend User model with more fields in Django?

Need help or resources for Django models.需要 Django 型号的帮助或资源。

Hello, I am trying to modify/extend the django.contrib.auth.models.User class so it could register a few more fields, apart from username and password .您好,我正在尝试修改/扩展django.contrib.auth.models.User class 以便它可以注册更多字段,除了usernamepassword For now I have implemented the User class only as a foreign key in another Task class.现在我已经实现了User class 仅作为另一个任务 class 中的外键。

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Task(models.Model):

    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True) # cascade deletes all tasks/items when user is deleted
    title = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    complete = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self) :
        return self.title

    
    class Meta:
        ordering = ['complete']

What I tried:我尝试了什么:

I've tried making it a One-to-One relation with a Employee class, but things got a little messy, as I was required to first register the user, and then later add attributes to the Employee as well as selecting the (already created) User as a primary key.我尝试将其与Employee class 建立一对一关系,但事情有点混乱,因为我需要先注册用户,然后再向Employee添加属性以及选择(已经创建) User作为主键。 That was not practical I suppose.我想那是不切实际的。

So my question is:所以我的问题是:

What is the best way to add attributes like Email, ID number, First name, Last name, etc in the User class and how to implement/render the appropriate form in the views.py ?User class 中添加 Email、ID 号、名字、姓氏等属性的最佳方法是什么,以及如何在views.py中实现/呈现适当的表单?

Here is my views.py file:这是我的views.py文件:

from django.contrib.auth.mixins import LoginRequiredMixin   # settings.py modification requried as following:
                                                            # add line: LOGIN_URL = 'login' above STATIC_URL field on line 120

from django.contrib.auth.forms import UserCreationForm      # for registration
from django.contrib.auth import login

from .models import Task

# Create your views here.
class CustomLoginView(LoginView):
    template_name = 'base/login.html'
    fields = '__all__'
    redirect_authenticated_user = True

    def get_success_url(self):           
        return reverse_lazy('tasks')    # Redirects to the homepage


class RegisterPage(FormView):
    template_name = 'base/register.html'
    form_class = UserCreationForm
    redirect_authenticated_user = True 
    success_url = reverse_lazy('tasks')

    def form_valid(self, form):
        user = form.save()
        if user is not None:
            login(self.request, user)
        return super(RegisterPage, self).form_valid(form)

    def get(self, *args, **kwargs):
        if self.request.user.is_authenticated:
            return redirect('tasks')
        
        return super(RegisterPage, self).get(*args, **kwargs)


class TaskList(LoginRequiredMixin ,ListView):
    model = Task 
    context_object_name = 'tasks'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['tasks'] = context['tasks'].filter(user=self.request.user)    # returns tasks from that user o.O
        context['count'] = context['tasks'].filter(complete=False).count()

        search_input = self.request.GET.get('search-area') or ''
        if search_input:
            context['tasks'] = context['tasks'].filter(title__icontains=search_input)

        context['search_input'] = search_input

        return context


class TaskDetail(LoginRequiredMixin, DetailView):      # LoginRequiredMixin has to be added for security
    model = Task 
    context_object_name = 'task'
    template_name = 'base/task.html'


class TaskCreate(LoginRequiredMixin, CreateView):
    model = Task
    fields = ['title', 'description', 'complete']
    success_url = reverse_lazy('tasks')

    def form_valid(self, form):
        form.instance.user = self.request.user 
        return super(TaskCreate, self).form_valid(form)


class TaskUpdate(LoginRequiredMixin, UpdateView):
    model = Task
    fields = ['title', 'description', 'complete']
    success_url = reverse_lazy('tasks')


class TaskDelete(LoginRequiredMixin, DeleteView):
    model = Task
    context_object_name = 'task'
    success_url = reverse_lazy('tasks')

and my file structure:和我的文件结构:

.
├── base
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_user.py
│   │   ├── 0003_delete_user.py
│   │   └── __init__.py
│   ├── models.py
│   ├── templates
│   │   └── base
│   │       ├── login.html
│   │       ├── register.html
│   │       ├── task.html
│   │       ├── task_confirm_delete.html
│   │       ├── task_form.html
│   │       └── task_list.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── manage.py
└── todo_list
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Thanks in advance: :)提前致谢: :)

You can inherit from the User model you are using or the AbstractUser model and create your own user model with as many fields as you like您可以从您正在使用的User model 或AbstractUser model 继承并创建您自己的用户 model 并使用任意数量的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM