django-users

AbstractUser Django full example

落花浮王杯 提交于 2019-12-03 07:34:51
问题 I am new to Django and I have been trying this for weeks, but could not find a way to solve this problem. I want to store additional information like user mobile number, bank name, bank account. And want to store the mobile number while user registers and wants user to login with either (mobile number and password) or (email and password). This is my UserProfile model from django.db import models from django.contrib.auth.models import User from django.contrib.auth.models import AbstractUser #

How to create a new user with django rest framework and custom user model

只愿长相守 提交于 2019-12-03 07:15:35
问题 I have a custom user model and I am using django-rest-framework to create API models.py: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField( unique=True, max_length=254, ) first_name = models.CharField(max_length=15) last_name = models.CharField(max_length=15) mobile = models.IntegerField(unique=True) date_joined = models.DateTimeField(default=timezone.now) is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) serializers.py: class

Django User model, adding function

房东的猫 提交于 2019-12-03 05:38:37
问题 I want to add a new function to the default User model of Django for retrieveing a related list of Model type. Such Foo model: class Foo(models.Model): owner = models.ForeignKey(User, related_name="owner") likes = models.ForeignKey(User, related_name="likes") ........ #at some view user = request.user foos= user.get_related_foo_models() How can this be achieved? 回答1: You can add a method to the User from django.contrib import auth auth.models.User.add_to_class('get_related_foo_models', get

Django: Hide button in template, if user is not super-user

只谈情不闲聊 提交于 2019-12-03 04:13:37
问题 How do you get your template/view to recognize whether or not a logged in user is a super user or not? There are certain buttons on my forms (in the template) that I want completely hidden if the user is not a super-user How would you go about doing that? 回答1: Check out is_superuser on the User object: {% if request.user.is_superuser %} ... <button>...</button> ... {% else %} ... {% endif %} EDIT: after @mustafa-0x comments The above assumes that you have django.core.context_processors

Extending User object in Django: User model inheritance or use UserProfile?

自闭症网瘾萝莉.ら 提交于 2019-12-03 02:57:42
To extend the User object with custom fields, the Django docs recommend using UserProfiles . However, according to this answer to a question about this from a year or so back: extending django.contrib.auth.models.User also works better now -- ever since the refactoring of Django's inheritance code in the models API. And articles such as this lay out how to extend the User model with custom fields, together with the advantages (retrieving properties directly from the user object, rather than through the .get_profile()). So I was wondering whether there is any consensus on this issue, or reasons

Setting up two different types of Users in Django 1.5/1.6

邮差的信 提交于 2019-12-03 01:32:28
问题 Please note--this is an updated version of my original question on this subject, but deserves to be asked again with the change in how Django deals with users and authentication. I'm working on a website with two very different kinds of users--let's call them Customers and Store Owners . Both register on the site, but have very different functionality. Customers simply have a single profile and can shop among the stores that they like. Store Owners have a single account but can have access to

AbstractUser Django full example

China☆狼群 提交于 2019-12-02 21:04:56
I am new to Django and I have been trying this for weeks, but could not find a way to solve this problem. I want to store additional information like user mobile number, bank name, bank account. And want to store the mobile number while user registers and wants user to login with either (mobile number and password) or (email and password). This is my UserProfile model from django.db import models from django.contrib.auth.models import User from django.contrib.auth.models import AbstractUser # Create your models here. class UserProfile(AbstractUser): user_mobile = models.IntegerField(max_length

How to create a new user with django rest framework and custom user model

青春壹個敷衍的年華 提交于 2019-12-02 20:50:05
I have a custom user model and I am using django-rest-framework to create API models.py: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField( unique=True, max_length=254, ) first_name = models.CharField(max_length=15) last_name = models.CharField(max_length=15) mobile = models.IntegerField(unique=True) date_joined = models.DateTimeField(default=timezone.now) is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) serializers.py: class UserSerializer(serializers.ModelSerializer): password1 = serializers.CharField(write_only=True)

Django 1.5 custom user model - signals limitation

做~自己de王妃 提交于 2019-12-02 19:32:39
问题 It's written in the doc that: Another limitation of custom User models is that you can’t use django.contrib.auth.get_user_model() as the sender or target of a signal handler. Instead, you must register the handler with the resulting User model. See Signals for more information on registering an sending signals. I guess it means you can do the following: from django.contrib.auth import get_user_model User = get_user_model() @receiver(post_save, sender=User) def user_saved(sender=None, instance

Django: Hide button in template, if user is not super-user

跟風遠走 提交于 2019-12-02 16:36:43
How do you get your template/view to recognize whether or not a logged in user is a super user or not? There are certain buttons on my forms (in the template) that I want completely hidden if the user is not a super-user How would you go about doing that? Check out is_superuser on the User object: {% if request.user.is_superuser %} ... <button>...</button> ... {% else %} ... {% endif %} EDIT: after @mustafa-0x comments The above assumes that you have django.core.context_processors.request included in your TEMPLATE_CONTEXT_PROCESSORS setting which isn't the default . The default setting for