You’ll want to review this reference:
https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey.on_delete
You probably want the option on doc to be SET_NULL.
Actually, I suggest some overall cleanup.
Your model names should follow a standard convention. Django practices the camel case convention. (CamelCase)
Also, you have a ForeignKey where you probably just want a OneToOneField.
Use related_name to have a reverse relationship. In my proposal below, you can access the profile picture just like you could before.
I would have just let Django handle the primary key field without changing the name of it.
doctor.profile_pic
class Doctor(models.Model):
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
class DoctorProfilePic (models.Model):
name = models.CharField(max_length=255, blank=True)
pic = StdImageField(upload_to="data/media/%Y/%m/%d", blank=True, variations={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
doctor = models.OneToOneField(Doctor, blank=True,
null=True, on_delete=models.SET_NULL, related_name="profile_pic")
…and after saying all that, I wouldn’t make a separate model for the profile picture. This is what I would do:
class Doctor(models.Model):
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
profile_pic = StdImageField(upload_to="data/media/%Y/%m/%d", blank=True, variations={
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
class Doctor(models.Model):
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
class DoctorProfilePic (models.Model):
name = models.CharField(max_length=255, blank=True)
pic = StdImageField(upload_to="data/media/%Y/%m/%d", blank=True, variations={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
doctor = models.OneToOneField(Doctor, blank=True,
null=True, on_delete=models.SET_NULL, related_name="profile_pic")
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/360df97b2ca14af69267bd6e98436ac6%40iss2.ISS.LOCAL.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com.
class Doctor(models.Model):
# When the profile picture gets delete, clear doctors profile picture (set to NULL)
profilepic = models.ForeignKey(DoctorProfilePic, blank=True, null=True, on_delete=models.SET_NULL)
class DoctorProfilePic (models.Model):
# When the doctor gets deleted, delete its picture too (CASCADE)
# PS. Do not allow orphan ProfilePics (w/o a doctor), i.e. blank=False, null=False
doc = models.ForeignKey('appointments.Doctor', on_delete=models.CASCADE)To post to this group, send email to djang...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/360df97b2ca14af69267bd6e98436ac6%40iss2.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c330d2a3-3fa1-458f-9c4d-519c70de3257%40googlegroups.com.