[英]Can I link my clients model with Django User models?
Hi im new using django and is a little confusing the part of model for my project now. 嗨我新使用django,现在我的项目有点混淆模型的一部分。
i need to create a site that user can have their profile and make post of product that they want to offer like a catalog and i'm thinking create this models: 我需要创建一个网站,用户可以拥有自己的个人资料,并制作他们想要提供的产品,如目录,我正在考虑创建这个模型:
class Users_Types(models.Model):
user_type = models.CharField(max_length=1)
def __str__(self):
return self.user_type
return self.id_type_user
class Users(models.Model):
users_types= models.OneToOneField('Users_Types')
e_mail = models.EmailField(unique=True)
password = models.TextField()
salt = models.TextField()
def __str__(self):
return self.id_user
return self.id_type_user
return self.e_mail
return self.password
return self.salt
class Clients(models.Model):
users = models.OneToOneField('Users')
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
ci = models.CharField(max_length=9)
birth_date = models.DateField(null=True, blank=True)
phone = models.CharField(max_length=20)
city = models.CharField(max_length=20, default=)
class products(models.Model):
clients = models.ForeignKey('clients')
product = models.CharField(max_length=30)
description = models.CharField(max_length=30)
Category = models.CharField(max_length=30)
but i read about django have it user model, i had to use that model instead that my Users model? 但我读到关于django有它的用户模型,我不得不使用该模型而不是我的用户模型?
and how i can link the django user model with my clients model. 以及如何将django用户模型与我的客户模型相关联。
i appreciate your help! 我感谢您的帮助!
Django indeed has it's User
model and all default authentication is made through that model, and from the start i would strongly recommend you to keep for that default Django User
model. Django确实拥有它的
User
模型,并且所有默认身份验证都是通过该模型进行的,从一开始我强烈建议您保留该默认的Django User
模型。
And for linking your Client with default user model you need to 并且要将您的客户端与您需要的默认用户模型相关联
from django.conf import settings
from django.db import models
class Client(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
...
Reading django docs on Using the Django authentication system would be very much useful for you. 阅读有关使用Django身份验证系统的 django文档对您非常有用。
I think that you know what PEP-8 is, and django has it's kinda same pep: Coding style , it would help you make your code much cleaner. 我想你知道PEP-8是什么,而且django有点像pep: 编码风格 ,它可以帮助你让你的代码更清洁。
Also Django docs has paragraph about referencing to User model: Referencing the User model 此外,Django文档中有关于引用用户模型的段落: 引用用户模型
Yes Django has it's own User built in module. 是的Django拥有自己的用户内置模块。 Just import Users first in your models file
只需在模型文件中首先导入用户
from django.contrib.auth.models import User
class UserProfile(models.Model):
users_types= models.ForeignKey(User)
e_mail = models.EmailField(unique=True)
password = models.TextField()
class Clients(models.Model):
user = models.ForeignKey(User)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
ci = models.CharField(max_length=9)
birth_date = models.DateField(null=True, blank=True)
phone = models.CharField(max_length=20)
city = models.CharField(max_length=20, default=)
You can get user by 你可以得到用户
user = User.objects.get(id=request.user.id)
request.user is containing your logged in user. request.user包含您登录的用户。 You can also get you client info by
您也可以通过以下方式获取客户信息
client_obj = Clients.objects.get(user=request.user)
Thank you 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.