Django配置文件模型:奇怪的错误:扩展Auth用户模型

时间:2017-02-01 14:49:47

标签: python django database django-models relational-database

由OneToOne字段引起的原因不明(我猜)

查看我的问题的底部

终端输出:

MappingContext mappingContext = .. // can be injected via Spring
Mapping mapping = (Mapping)mappingContext.getPersistentEntity(Foo).mapping.mappedForm

models.py:

In [21]: profile = User.objects.get(email='jlennon@beatles.com')
In [22]: profile
Out[22]: <User: jlennon@beatles.com>

In [23]: profile = User.objects.get(email='jlennon@beatles.com').profile

In [24]: profile
Out[24]: <Users: Aniket>
In [25]: profile = User.objects.get(id=2).profile

In [26]: profile
Out[26]: <Users: Aniket>

In [27]: User.objects.get(id=2)
Out[27]: <User: jlennon@beatles.com>

In [28]: User.objects.get(id=4)
Out[28]: <User: AniketYadav>

settings.py

class Users(models.Model):
    user = models.OneToOneField(User, related_name='profile')
    user_Id = models.BigAutoField(primary_key=True)
    user_name = models.CharField(max_length=25)
    user_fname = models.CharField(max_length=40, blank=True, null=True)
    user_lname = models.CharField(max_length=40, blank=True, null=True)
    user_email = models.CharField(max_length=60)
    user_password = models.CharField(max_length=255)
    joining_date = models.DateTimeField()
    user_dob = models.DateField()
    user_country = models.CharField(max_length=3, blank=True, null=True)
    user_gender = models.CharField(max_length=1)
    user_pic = models.CharField(max_length=255, blank=True, null=True)
    user_about = models.CharField(max_length=512, blank=True, null=True)

    class Meta:
        verbose_name = 'Users'
        verbose_name_plural = 'Users'
        managed = False
        db_table = 'tbl_users'

    def __unicode__(self):
        return self.user_name

    def __str__(self):
        return self.user_name

我的数据库:

AUTH_PROFILE_MODULE = 'myWebsite.Users'

我的用户表(tbl_users):

mysql> select id, username, email from auth_user where id=1 or id=2 or id=4;
+----+---------------------+---------------------+
| id | username            | email               |
+----+---------------------+---------------------+
|  1 | admin               | admin@admin.com     |
|  2 | jlennon@beatles.com | jlennon@beatles.com |
|  4 | AniketYadav         | aniket@gmail.com    |
+----+---------------------+---------------------+
3 rows in set (0.00 sec)

我认为,因为我没有+---------+-----------+------------------------------+ | user_id | user_name | user_email | +---------+-----------+------------------------------+ | 2 | Aniket | aniket@gmail.com | | 3 | Crazy | crazy@crazy.com | +---------+-----------+------------------------------+ 2 rows in set (0.00 sec) 中的用户,因此我可能会收到错误的输出。我是对的吗?

OneToOneField 如何将tbl_users表从User链接到django.contrib.auth.mmodels应用Users

我认为上述问题的答案可能会让我明白如何继续制作个人资料模型。

models.py表格来自旧的 PHP 项目,我不想放弃,这就是为什么它仍有tbl_users字段的原因&#39 ; s模型(因为它已使用 inspectdb 导入。

然而,我会在注册时创建配置文件,因为我已经阅读了某个地方,我认为那时我不必担心这个问题,因为我会为每个配置文件提供配置文件对象。

但我仍然想知道如何纠正正在进行的情况我做错了什么

编辑:

建议更改的用户

user_password

MySQL表格说明

class Users(models.Model):
    user = models.OneToOneField(User, related_name='profile')
    user_Id = models.IntegerField()
    user_name = models.CharField(max_length=25)
    user_fname = models.CharField(max_length=40, blank=True, null=True)
    user_lname = models.CharField(max_length=40, blank=True, null=True)
    user_email = models.CharField(primary_key=True, max_length=60)
    user_password = models.CharField(max_length=255)
    joining_date = models.DateTimeField()
    user_dob = models.DateField()
    user_country = CountryField()
    user_gender = models.CharField(max_length=1)
    user_pic = models.CharField(max_length=255, blank=True, null=True)
    user_about = models.CharField(max_length=512, blank=True, null=True)

    class Meta:
        verbose_name = 'Users'
        verbose_name_plural = 'Users'
        managed = False
        db_table = 'tbl_users'

    def __unicode__(self):
        return self.user_name

    def __str__(self):
        return self.user_name

但我得到的输出仍然是:

mysql> desc tbl_users;
+---------------+--------------+------+-----+------------------------------+-------+
| Field         | Type         | Null | Key | Default                      | Extra |
+---------------+--------------+------+-----+------------------------------+-------+
| user_id       | int(11)      | NO   |     | NULL                         |       |
| user_name     | varchar(25)  | NO   |     | NULL                         |       |
| user_fname    | varchar(40)  | YES  |     | NULL                         |       |
| user_lname    | varchar(40)  | YES  |     | NULL                         |       |
| user_email    | varchar(60)  | NO   | PRI | NULL                         |       |
| user_password | varchar(255) | NO   |     | NULL                         |       |
| joining_date  | datetime     | NO   |     | NULL                         |       |
| user_dob      | date         | NO   |     | NULL                         |       |
| user_country  | varchar(3)   | YES  |     | NULL                         |       |
| user_gender   | char(1)      | NO   |     | NULL                         |       |
| user_pic      | char(255)    | YES  |     | ../images/provideAnImage.jpg |       |
| user_about    | varchar(512) | YES  |     | NULL                         |       |
+---------------+--------------+------+-----+------------------------------+-------+
12 rows in set (0.00 sec)

当我试图查看后面的查询时:

In [4]: User.objects.get(email='aniket@gmail.com').profile
Out[4]: <Users: Apurva>
In [5]: User.objects.get(email='jlennon@beatles.com')
Out[5]: <User: jlennon@beatles.com>

In [6]: User.objects.get(email='jlennon@beatles.com').profile
Out[6]: <Users: Aniket>

所以我找到了另一种选择

但请告诉我一个更好的方法......

In [7]: User.objects.get(email='jlennon@beatles.com').profile.query
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-201041bc7f1c> in <module>()
----> 1 User.objects.get(email='jlennon@beatles.com').profile.query

AttributeError: 'Users' object has no attribute 'query'

In [8]: User.objects.filter(email='jlennon@beatles.com').profile.query
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-c4827b8b2ee1> in <module>()
----> 1 User.objects.filter(email='jlennon@beatles.com').profile.query

AttributeError: 'QuerySet' object has no attribute 'profile'

In [9]: User.objects.filter(email='jlennon@beatles.com').query
Out[9]: <django.db.models.sql.query.Query at 0x7fefe8741f90>

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找 AUTH_USER_MODEL

您已指定AUTH_PROFILE_MODEL,但只要您不指定AUTH_USER_MODEL,Django将仅使用其内置的用户模型。

编辑:

不幸的是我无法深入证明,但似乎django正在主键上构建外键关系查询。实际上是

的查询
User.objects.get(email='jlennon@beatles.com').profile 

变得类似于:

SELECT tu.id FROM auth_user as au JOIN tbl_users as tu ON au.id=tu.id WHERE au.email='jlennon@beatles.com';

但是,id=2 jlennon@beatles.comtbl_users匹配后,它会抓取与2对应的Aniket对象。这就是造成这种奇怪行为的原因。我相信在用户模型中设置 email as PK 可以为您解决问题。

如何证明?

您可以尝试运行控制台中的操作并向其添加.query以查找Django运行的确切sql查询。对于ex:User.objects.get(email='jlennon@beatles.com').profile.query,在这种情况下,它必须与上面的查询类似。

希望这会有所帮助。还要考虑AUTH_PROFILE_MODULE是否已折旧。鉴于你的情况,我把它留给你。