简体   繁体   中英

Django array field foreign key

I have extended my user mode with a profile that has an optional field that is an array. Each element in the array can be is a unique value from a field in another table. In that specific table, I have a field that is an array field that I want to hold the email addresses of the users that have that option in their array field. Is there a way that I can make it to where that field contains the email addresses of the users that have that particular fields value in their array field? And if that element gets removed from the user profile array field it gets removed from that table's array as well?

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.

I'm basically wanting to link the email address in the user table to the emails field in the LocationCodes table if that user has that particular location in their locations array field.

as much i understand your question, (because you did not share any code or model) you can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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