gpt4 book ai didi

mysql - Django : OneToOne field links to id and not to email

转载 作者:行者123 更新时间:2023-11-29 19:27:16 25 4
gpt4 key购买 nike

如何更改 OneToOne 字段的链接

请参阅底部以了解我的问题

终端输出:

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>

models.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

settings.py

AUTH_PROFILE_MODULE = 'myWebsite.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)

我的用户表 (tbl_users):

+---------+-----------+------------------------------+
| user_id | user_name | user_email |
+---------+-----------+------------------------------+
| 2 | Aniket | aniket@gmail.com |
| 3 | Crazy | crazy@crazy.com |
+---------+-----------+------------------------------+
2 rows in set (0.00 sec)

我这样做了,但没有成功

建议更改的新用户

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 表说明:

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>

最佳答案

对此有一个解决方案,您可以发送电子邮件

在你的项目根目录下,你可以看到__init__.py添加以下代码。

from django.contrib.auth.models import User
User._meta.get_field('email')._unique = True

关于mysql - Django : OneToOne field links to id and not to email,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com