Djnago how to split users and customres
In my project (small online shop) I need to split registration for users and customers.
So the information what I found when somebody registered in django then his account stored in one table, in this table I can see admin user and staff and another registered accounts, and I can sse them all in admin on Users page. But I do not want ot put all accounts in one "basket". I need split them fro different tables.
For example superuser can create in admin area a new user (content manager) and provide him access/permission to manage admin area (create product etc.) - this users and super user will be on default User page. On the page Customers will be displaying only users who registered for example via https://mysite/account/register page, after registration this customer account I can see in Customers page in the admin area but not in Users page. And this customer can login to his account for example via https://mysite/account/login
Is this possible?
As Jay said, everyone registered in the database is still a User whatever their role may be (admin, superuser, customer). What you could do is create a Profile model where everyone will have their information such as telephone, location etc, and you will also add another field clarifying their property.
PACKAGES = [
('customer', 'Customer'),
('support', 'Support'),
('admin', 'Admin'),
]
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
image = models.ImageField(default='user_avatar.png', upload_to='...')
last_visit = models.DateField(default=timezone.now, blank=True)
location = models.CharField(max_length=254, null=True, blank=True)
contact_phone = models.CharField(max_length=15)
user_role = models.CharField(default="customer", choices=PACKAGES, max_length=20)
Then all you need to do is edit your admin.py to implement a search parameter there:
class ProfileAdmin(admin.ModelAdmin):
list_filter=('user_role',)
admin.site.register(Profile, ProfileAdmin)
Doing that will give you a filter_list in the right corner of your admin page but that is for admin page only.
If you want to access different roles in your views or your templates you will do so by getting the user_role you need:
customers = Profile.objects.filter(user_role='customer')