• django扩展User模型(model),profile


    from django.contrib.auth.models import User
    
    
    # Create your models here.
    
    
    class Profile(models.Model):
        user = models.OneToOneField(User, related_name='profile')
        date_of_birth = models.DateTimeField(blank=True, null=True)
        photo = models.ImageField(upload_to='user/%Y/%m/%d', blank=True)
    
        def __str__(self):
            return 'Profile for user {}'.format(self.user.username)

    修改setting.py

    AUTH_PROFILE_MODULE = 'account.Profile'  # app名.model名

    按照下面方式调用

    @login_required
    def edit(request):
        if request.method == 'POST':
            user_form = UserEditForm(instance=request.user,
                                     data=request.POST)
            profile_form = ProfileEditForm(instance=request.user.profile,
                                           data=request.POST,
                                           files=request.FILES)
            if user_form.is_valid() and profile_form.is_valid():
                user_form.save()
                profile_form.save()
        else:
            user_form = UserEditForm(instance=request.user)
            profile_form = ProfileEditForm(instance=request.user.profile)
        return render(request,
                      'account/edit.html',
                      {'user_form': user_form,
                       'profile_form': profile_form})
  • 相关阅读:
    37 图的存储结构
    hdu 1272 使用set和并查集
    题目数据输入中间读入字符
    第六篇 模块基础
    第十二章 并发编程
    并发编程(四)
    第五篇 函数进阶
    第四篇 函数基础
    并发编程(三)
    并发编程(二)
  • 原文地址:https://www.cnblogs.com/gange111/p/9599228.html
Copyright © 2020-2023  润新知