In the app accounts I created in Django, I have three models. student, Adviser and PhoneNumber.
Student model:
class Student(models.Model):
profile_image = models.ImageField(
max_length=255,
upload_to=get_student_profile_img_upload_path,
null=True,
blank=True,
default=get_student_default_profile_image,
)
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
adviser = models.ForeignKey(Adviser, on_delete=models.CASCADE, related_name='adviser')
educational_stage = models.CharField(choices=get_student_educational_stage(), max_length=4)
grade = models.CharField(choices=get_student_grade(), max_length=10)
field = models.CharField(choices=get_student_field(), max_length=21, null=True, blank=True)
birthday_date = models.DateField(null=True)
phone_number = models.ForeignKey(PhoneNumber, null=True, on_delete=models.SET_NULL)
province = models.CharField(choices=PROVINCE_CHOICES, max_length=100)
city = models.CharField(choices=CITY_CHOICES, max_length=100)
Advisor model:
class Adviser(models.Model):
profile_image = models.ImageField(
max_length=255,
upload_to=get_adviser_profile_img_upload_path,
null=True,
blank=True,
default=get_adviser_default_profile_image,
)
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
phone_number = models.ForeignKey(PhoneNumber, null=True, on_delete=models.SET_NULL)
province = models.CharField(choices=PROVINCE_CHOICES, max_length=100)
city = models.CharField(choices=CITY_CHOICES, max_length=100)
PhoneNumber model:
class PhoneNumber(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
phone_number = PhoneNumberField(null=False, blank=False, unique=True)
I plan to have the phone number in a separate table, but when I want to create a form in forms.py, I have to use the student or advisor model. While I want the phone number to be stored in the phone number table and displayed together with the fields of the student or adviser model in form.
in other words, I need to receive the phone number along with the student or adviser model fields, but it must also be stored in the PhoneNumber table.
What method do you suggest for doing this?