账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    django 当中创建表,auth_user表和users_userprofile表不能共存么?
    33
    0

    django 当中创建表,auth_user表和users_userprofile表不能共存么?为什么有auth_user表创建users_userprofile表就不出来,有users_userprofile表的话auth_user表就会消失。

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 九宫衔蝉 普通会员 1楼

      在 Django 中,auth_userusers_userprofile 表通常被用于存储用户信息,但它们的字段可能会有不同的结构和逻辑,因此不能简单地将它们视为共存的表。

      auth_user 表通常包含以下字段:

      1. id:用户 ID,通常为整数。
      2. username:用户用户名。
      3. email:用户电子邮件地址。
      4. password:用户密码(如果包含密码,为可选字段)。
      5. is_active:用户是否活跃(True 或 False)。
      6. is_superuser:是否超级用户(True 或 False)。
      7. last_login:最后一次登录日期和时间。
      8. first_name:用户的第一个全名。
      9. last_name:用户的最后一个全名。
      10. date_joined:用户加入的日期和时间。
      11. profile_picture:用户头像路径。

      users_userprofile 表通常包含以下字段:

      1. id:用户 ID,通常为整数。
      2. username:用户用户名。
      3. email:用户电子邮件地址。
      4. user_id:用户 ID,通常为 auth_userid
      5. password:用户密码(如果包含密码,为可选字段)。
      6. user_profile:用户个人资料,如个人头像、个人描述等。
      7. profile_picture:用户头像路径。

      由于 auth_userusers_userprofile 表的主要关注点在于用户信息的存储,它们的字段结构和逻辑有所不同。因此,如果想要将这两个表共存,你需要根据实际需求进行以下处理:

      1. 创建一个独立的视图模块,将 auth_userusers_userprofile 作为两个表进行查询和操作。例如:

      ```python from django.db import models from .models import AuthUser, UserProfile

      class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) profile_picture = models.ImageField(upload_to='user_profiles/', blank=True)

      class AuthUser(models.Model): username = models.CharField(max_length=255) email = models.EmailField() password = models.CharField(max_length=255) is_active = models.BooleanField(default=True) is_superuser = models.BooleanField(default=False) date_joined = models.DateTimeField(auto_now_add=True) first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) profile_picture = models.ImageField(upload_to='auth_user_profiles/', blank=True)

      def __str__(self):
          return f'{self.username} ({self.first_name} {self.last_name})'
      

      使用视图模块创建模型类

      auth_user = models.Model() users_userprofile = models.Model()

      auth_user_profile.objects.create(user=auth_user) users_userprofile.user = auth_user ```

      在上面的示例中,我们首先创建了两个模型类 AuthUserUserProfile,分别用于存储 auth_userusers_userprofile 表中的数据。然后,我们使用 auth_user_profile.objects.create(user=auth_user)users_userprofile.user = auth_userauth_userusers_userprofile 保存到数据库中。这样,auth_userusers_userprofile 表就可以共存了,且它们的字段结构和逻辑都正确地反映了两个表的结构和关系。

      注意,这种方式需要你为每个模型类创建单独的模型类,这样可以确保模型之间的独立性和一致性,避免在同一个视图模块中同时查询和操作多个表。如果需要将多个表作为一个视图模块中的模型进行操作,可以在视图模块中为每个表创建一个独立的模型类,然后在查询和操作这些表时分别使用对应模型类。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部