简体   繁体   English

我可以将我的客户端模型与Django用户模型链接吗?

[英]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.

相关问题 我可以将我的客户模型链接到我的用户模型吗? - Can I link my customer model to my user model? 如何将注册用户从django-registration链接到我的应用程序模型? - How can I link a registered user from django-registration to the model of my app? 我可以将Django权限链接到模型类而不是用户实例吗? - Can I link Django permissions to a model class instead of User instances? 如何链接/或组合我的三个 Django 模型以更好地最大化关系和效率 - How can I link/or combine my three Django models to better maximize relationship and efficency 我不能在django.contrib.auth.models的用户模型中添加一些列吗? - Can't I add some columns to User model of django.contrib.auth.models? Django 2.1-无法在html文件中链接我的模型变量 - Django 2.1 - can't link my models variables in html file 创建用户后,如何将用户与 django model 链接? - How do I link a User with a django model as soon as the User is created? 如何将django-avatar与我自己的用户模型一起使用,而不与django.contrib.auth.models.User一起使用 - How to use django-avatar with my own User model and not with django.contrib.auth.models.User 当在另一个 Django Model 中找到用户时,如何在列表中显示链接 - How can I Display Link in List when User is found in another Django Model 当用户 model 更新 Django 时,如何更新我的用户配置文件 model - How can I update my UserProfile model when the User model is updated Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM