繁体   English   中英

Django数组字段外键

[英]Django array field foreign key

我用配置文件扩展了用户模式,该配置文件具有一个可选字段,该字段是一个数组。 数组中的每个元素可以是另一个表中字段的唯一值。 在该特定表中,我有一个字段,它是一个数组字段,我想在其数组字段中保存具有该选项的用户的电子邮件地址。 有什么办法可以使我到达该字段包含其数组字段中具有该特定字段值的用户的电子邮件地址的位置? 如果该元素从用户配置文件数组字段中删除,那么它也从该表的数组中删除吗?

class LocationCodes(models.Model):
    location = models.CharField(db_column='location', primary_key=True, max_length=10)# Abbreviated location code
    customergroup = ArrayField(models.CharField(db_column='customergroup', max_length=6))
    emails = ArrayField(models.CharField(db_column='emails', max_length=60)) #Array field that I want to link to User Model emails
    location_name = models.CharField(db_column='location_name', max_length=60)
    state = models.CharField(db_column='state', max_length=20)


class UserProfileExtension(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    locations = MultiSelectField(choices=location_code_choices) #This field contains an array of the locations that are from the location field in the LocationCodes table.

我基本上是想将用户表中的电子邮件地址链接到LocationCodes表中的电子邮件字段,如果该用户在其locations数组字段中具有该特定位置。

据我了解您的问题,(因为您没有共享任何代码或模型),您可以尝试以下操作:

from django.contrib.auth.models import User
email_address = models.ForeignKey(User, on_delete=models.CASCADE)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM