gpt4 book ai didi

python - Django-ManyToMany表提取了错误的信息

转载 作者:行者123 更新时间:2023-11-29 20:42:01 25 4
gpt4 key购买 nike

我正在尝试为这两个表之间的多对多关系创建一个链接器表。第三个表将具有第一个表中的cand_id和第二个表中的p_id。这是我的候选代码中的代码,MariaDB的模型和表中的P_user classes。当我在候选模型中使用ManyToManyField选项时,它会不断提取用户的名字而不是用户的p_id

第一个模型:

class P_user(models.Model):
''' Proxy database model to extend Django User with profile data.'''

user = models.ForeignKey(User)
p_id = models.CharField(primary_key = True)
phone = models.CharField(max_length = 20,
default = '',
blank = True)
password = models.CharField(max_length = 30,
default = '',
blank = True)
confirm_password = models.CharField(max_length = 30,
default = '',
blank = True)
roles = models.CharField(max_length = 30,
default = '',
blank = True)


第二种模式:

class Candidate(models.Model)

....
associated_hm = models.ManyToManyField(P_user,
blank = True)
....


数据库表示:

MariaDB [mantis]> describe prog_port_candidate;

+------------------------+--------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+------------------------+--------------+------+-----+---------+----------------+

| cand_id | int(11) | NO | PRI | NULL | auto_increment |

| first_name | varchar(20) | NO | | NULL | |

| last_name | varchar(20) | NO | | NULL | |


+------------------------+--------------+------+-----+---------+----------------+


下一个:

MariaDB [mantis]> describe prog_port_p_user;
+------------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------------+-------------+------+-----+---------+-------+

| p_id | varchar(8) | NO | PRI | NULL | |

| phone | varchar(20) | NO | | NULL | |

| password | varchar(30) | NO | | NULL | |

| confirm_password | varchar(30) | NO | | NULL | |

| roles | varchar(30) | NO | | NULL | |

| user_id | int(11) | NO | MUL | NULL | |

+------------------+-------------+------+-----+---------+-------+

最佳答案

这可能是一个漫长的过程,因为您的问题并未使您不正确的输出的上下文清晰可见,但是如果您仅引用yourmodel.associated_hm,则将看到对associated_hm对象的引用。

要获取p_id,您需要像yourmodel.associated_hm.p_id这样在查询中显式引用它。

关于python - Django-ManyToMany表提取了错误的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38590751/

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