0

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.

dxt
  • 122
  • 1
  • 3
  • 13
Jasonca1
  • 4,848
  • 6
  • 25
  • 42
  • take a look at my answer here: https://stackoverflow.com/questions/1110153/what-is-the-most-efficient-way-to-store-a-list-in-the-django-models/49871886#49871886 And if that helped then don't forget to upvote it :) – Ahtisham Feb 05 '19 at 10:16

1 Answers1

0

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)
Cathrine
  • 131
  • 10
  • if you wanna get email address from default User table using above code you can get email address in your 2nd table – Cathrine Feb 05 '19 at 09:25