gpt4 book ai didi

python - Django - 需要一些关于如何实现 ManyToMany 数据库的指导

转载 作者:太空宇宙 更新时间:2023-11-04 01:42:06 25 4
gpt4 key购买 nike

我希望能够让用户指出他们访问过的国家/地区。我的 models.py 看起来像这样:

class User(models.Model):
name = models.CharField(max_length=50)
countries = models.ManyToManyField(Countries)

class Countries(models.Model):
#This is where I don't know what to do.
#The end goal is for the user to be able to check off what countries he/she has visited

最佳答案

你会以相反的方式建立关系

 class User(models.Model):
name = models.CharField(max_length=50)

class Countries(models.Model):
user = models.ForeignKey(User)

如果您正在使用 django 的内置用户内容,那么您只需要这个。

from django.contrib.auth.models import User

class Countries(models.Model):
user = models.ForeignKey(User)

关于python - Django - 需要一些关于如何实现 ManyToMany 数据库的指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3973408/

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