Discussion:
Unexpected behavior on delete of model
Joel Mathew
2018-11-27 18:23:54 UTC
Permalink
Situation:

I have two Model classes, in two different apps which are part of the same
project. class doctor defined in appointments.models is a set of attributes
associated with a doctor, like name, username, email, phone etc. class
DoctorProfilePic is a Model defined in clinic.models, which has a
StdImageField which stores images. doctor has a One to One mapping to
DoctorProfilePic.

class doctor(models.Model):
docid = models.AutoField(primary_key=True, unique=True) # Need
autoincrement, unique and primary
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
...
profilepic = models.ForeignKey(DoctorProfilePic, blank=True,
null=True, on_delete=models.CASCADE)
...

class DoctorProfilePic (models.Model):
id = models.AutoField(primary_key=True, unique=True)
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),
})
doc = models.ForeignKey('appointments.doctor', blank=True,
null=True, on_delete=models.CASCADE)

Anticipated response:

When user selects one of the profile pics from a selection box, and clicks
Delete, django is supposed to delete the picture from the collection of
pictures uploaded by the doctor.

Problem:

When the delete button is clicked, django deletes both the picture and the
doctor, instead of just the former.

Code:

def removeprofpic(request, docid):
docid = int(docid)
if not IsOwnerorSuperUser(request, docid):
return HttpResponse("You dont have permissions to do this.")
doc = doctor.objects.get(docid = docid)
if request.method == 'POST':
print(request.POST)
picid = int(request.POST.get('profilepic'))
print(f'doc:{doc} picid:{picid}')
pic = DoctorProfilePic.objects.get(doc = doc, id =picid)
pic.delete()
msg = f"Successfully removed profile picture."
else:
msg = "Not a valid POST"
return HttpResponse(msg)

Now I'm guessing my problem is in defining the on_delete=models.CASCADE?
Can someone explain what I've done wrong?
Sincerely yours,

Joel G Mathew
--
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.
Matthew Pava
2018-11-27 19:48:07 UTC
Permalink
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={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})



From: django-***@googlegroups.com [mailto:django-***@googlegroups.com] On Behalf Of Joel Mathew
Sent: Tuesday, November 27, 2018 12:24 PM
To: django-***@googlegroups.com
Subject: Unexpected behavior on delete of model

Situation:

I have two Model classes, in two different apps which are part of the same project. class doctor defined in appointments.models is a set of attributes associated with a doctor, like name, username, email, phone etc. class DoctorProfilePic is a Model defined in clinic.models, which has a StdImageField which stores images. doctor has a One to One mapping to DoctorProfilePic.

class doctor(models.Model):
docid = models.AutoField(primary_key=True, unique=True) # Need autoincrement, unique and primary
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
...
profilepic = models.ForeignKey(DoctorProfilePic, blank=True, null=True, on_delete=models.CASCADE)
...

class DoctorProfilePic (models.Model):
id = models.AutoField(primary_key=True, unique=True)
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),
})
doc = models.ForeignKey('appointments.doctor', blank=True,
null=True, on_delete=models.CASCADE)

Anticipated response:

When user selects one of the profile pics from a selection box, and clicks Delete, django is supposed to delete the picture from the collection of pictures uploaded by the doctor.

Problem:

When the delete button is clicked, django deletes both the picture and the doctor, instead of just the former.

Code:

def removeprofpic(request, docid):
docid = int(docid)
if not IsOwnerorSuperUser(request, docid):
return HttpResponse("You dont have permissions to do this.")
doc = doctor.objects.get(docid = docid)
if request.method == 'POST':
print(request.POST)
picid = int(request.POST.get('profilepic'))
print(f'doc:{doc} picid:{picid}')
pic = DoctorProfilePic.objects.get(doc = doc, id =picid)
pic.delete()
msg = f"Successfully removed profile picture."
else:
msg = "Not a valid POST"
return HttpResponse(msg)

Now I'm guessing my problem is in defining the on_delete=models.CASCADE? Can someone explain what I've done wrong?
Sincerely yours,
Joel G Mathew

--
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<mailto:django-users+***@googlegroups.com>.
To post to this group, send email to django-***@googlegroups.com<mailto: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<https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
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.
Saurabh Agrawal
2018-11-28 05:36:48 UTC
Permalink
Post by Joel Mathew
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
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")
Setting on_delete to SET_NULL doesn't make much sense here, I think. This
implies that if a doctor is deleted, the profile pic will be preserved,
with the FK to doc being set to NULL in db (which doesn't seem desirable).
I think here CASCADE should work good and will not have the issue that the
OP is facing.
Post by Joel Mathew

and after saying all that, I wouldn’t make a separate model for the
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={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
*Sent:* Tuesday, November 27, 2018 12:24 PM
*Subject:* Unexpected behavior on delete of model
I have two Model classes, in two different apps which are part of the same
project. class doctor defined in appointments.models is a set of attributes
associated with a doctor, like name, username, email, phone etc. class
DoctorProfilePic is a Model defined in clinic.models, which has a
StdImageField which stores images. doctor has a One to One mapping to
DoctorProfilePic.
docid = models.AutoField(primary_key=True, unique=True) # Need
autoincrement, unique and primary
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
...
profilepic = models.ForeignKey(DoctorProfilePic, blank=True,
null=True, on_delete=models.CASCADE)
...
id = models.AutoField(primary_key=True, unique=True)
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),
})
doc = models.ForeignKey('appointments.doctor', blank=True,
null=True, on_delete=models.CASCADE)
When user selects one of the profile pics from a selection box, and clicks
Delete, django is supposed to delete the picture from the collection of
pictures uploaded by the doctor.
When the delete button is clicked, django deletes both the picture and the
doctor, instead of just the former.
docid = int(docid)
return HttpResponse("You dont have permissions to do this.")
doc = doctor.objects.get(docid = docid)
print(request.POST)
picid = int(request.POST.get('profilepic'))
print(f'doc:{doc} picid:{picid}')
pic = DoctorProfilePic.objects.get(doc = doc, id =picid)
pic.delete()
msg = f"Successfully removed profile picture."
msg = "Not a valid POST"
return HttpResponse(msg)
Now I'm guessing my problem is in defining the on_delete=models.CASCADE?
Can someone explain what I've done wrong?
Sincerely yours,
Joel G Mathew
--
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
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
<https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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
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
<https://groups.google.com/d/msgid/django-users/360df97b2ca14af69267bd6e98436ac6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>
.
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/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Joel
2018-11-28 06:04:09 UTC
Permalink
I think you made a typo in the code. doctor can't be a Foreign key for
class Doctor. Anyway I get your point, and this is the same way I solved it
yesterday.
Post by Saurabh Agrawal
Post by Joel Mathew
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
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")
Setting on_delete to SET_NULL doesn't make much sense here, I think. This
implies that if a doctor is deleted, the profile pic will be preserved,
with the FK to doc being set to NULL in db (which doesn't seem desirable).
I think here CASCADE should work good and will not have the issue that the
OP is facing.
Post by Joel Mathew

and after saying all that, I wouldn’t make a separate model for the
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={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
*Sent:* Tuesday, November 27, 2018 12:24 PM
*Subject:* Unexpected behavior on delete of model
I have two Model classes, in two different apps which are part of the
same project. class doctor defined in appointments.models is a set of
attributes associated with a doctor, like name, username, email, phone etc.
class DoctorProfilePic is a Model defined in clinic.models, which has a
StdImageField which stores images. doctor has a One to One mapping to
DoctorProfilePic.
docid = models.AutoField(primary_key=True, unique=True) # Need
autoincrement, unique and primary
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
...
profilepic = models.ForeignKey(DoctorProfilePic, blank=True,
null=True, on_delete=models.CASCADE)
...
id = models.AutoField(primary_key=True, unique=True)
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),
})
doc = models.ForeignKey('appointments.doctor', blank=True,
null=True, on_delete=models.CASCADE)
When user selects one of the profile pics from a selection box, and
clicks Delete, django is supposed to delete the picture from the collection
of pictures uploaded by the doctor.
When the delete button is clicked, django deletes both the picture and
the doctor, instead of just the former.
docid = int(docid)
return HttpResponse("You dont have permissions to do this.")
doc = doctor.objects.get(docid = docid)
print(request.POST)
picid = int(request.POST.get('profilepic'))
print(f'doc:{doc} picid:{picid}')
pic = DoctorProfilePic.objects.get(doc = doc, id =picid)
pic.delete()
msg = f"Successfully removed profile picture."
msg = "Not a valid POST"
return HttpResponse(msg)
Now I'm guessing my problem is in defining the on_delete=models.CASCADE?
Can someone explain what I've done wrong?
Sincerely yours,
Joel G Mathew
--
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
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
<https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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
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
<https://groups.google.com/d/msgid/django-users/360df97b2ca14af69267bd6e98436ac6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>
.
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
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/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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/CAA%3Diw_8ObhXgL-n%3Dkd5-8RN9En_z9td8UPrefWmQXt7we21tTA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Joel
2018-11-28 06:05:22 UTC
Permalink
Ignore the last post. Formatting blues in my main editor.
Post by Joel
I think you made a typo in the code. doctor can't be a Foreign key for
class Doctor. Anyway I get your point, and this is the same way I solved it
yesterday.
Post by Saurabh Agrawal
Post by Joel Mathew
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
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")
Setting on_delete to SET_NULL doesn't make much sense here, I think. This
implies that if a doctor is deleted, the profile pic will be preserved,
with the FK to doc being set to NULL in db (which doesn't seem desirable).
I think here CASCADE should work good and will not have the issue that the
OP is facing.
Post by Joel Mathew

and after saying all that, I wouldn’t make a separate model for the
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={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
*Sent:* Tuesday, November 27, 2018 12:24 PM
*Subject:* Unexpected behavior on delete of model
I have two Model classes, in two different apps which are part of the
same project. class doctor defined in appointments.models is a set of
attributes associated with a doctor, like name, username, email, phone etc.
class DoctorProfilePic is a Model defined in clinic.models, which has a
StdImageField which stores images. doctor has a One to One mapping to
DoctorProfilePic.
docid = models.AutoField(primary_key=True, unique=True) # Need
autoincrement, unique and primary
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
...
profilepic = models.ForeignKey(DoctorProfilePic, blank=True,
null=True, on_delete=models.CASCADE)
...
id = models.AutoField(primary_key=True, unique=True)
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),
})
doc = models.ForeignKey('appointments.doctor', blank=True,
null=True, on_delete=models.CASCADE)
When user selects one of the profile pics from a selection box, and
clicks Delete, django is supposed to delete the picture from the collection
of pictures uploaded by the doctor.
When the delete button is clicked, django deletes both the picture and
the doctor, instead of just the former.
docid = int(docid)
return HttpResponse("You dont have permissions to do this.")
doc = doctor.objects.get(docid = docid)
print(request.POST)
picid = int(request.POST.get('profilepic'))
print(f'doc:{doc} picid:{picid}')
pic = DoctorProfilePic.objects.get(doc = doc, id =picid)
pic.delete()
msg = f"Successfully removed profile picture."
msg = "Not a valid POST"
return HttpResponse(msg)
Now I'm guessing my problem is in defining the on_delete=models.CASCADE?
Can someone explain what I've done wrong?
Sincerely yours,
Joel G Mathew
--
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
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
<https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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
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
<https://groups.google.com/d/msgid/django-users/360df97b2ca14af69267bd6e98436ac6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>
.
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
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/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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/CAA%3Diw_8p420iBYmK8hoVZgqX_FjLu__8ONNBXm%2BENJouaC8uTA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Todor Velichkov
2018-11-28 07:16:14 UTC
Permalink
Right now, your doctor will be deleted if its ProfilePicture gets deleted.
I'm pretty sure you don't want to do that.
I believe this is what you are looking for:

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)
Post by Joel
Ignore the last post. Formatting blues in my main editor.
Post by Joel
I think you made a typo in the code. doctor can't be a Foreign key for
class Doctor. Anyway I get your point, and this is the same way I solved it
yesterday.
Post by Saurabh Agrawal
Post by Joel Mathew
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
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")
Setting on_delete to SET_NULL doesn't make much sense here, I think.
This implies that if a doctor is deleted, the profile pic will be
preserved, with the FK to doc being set to NULL in db (which doesn't seem
desirable). I think here CASCADE should work good and will not have the
issue that the OP is facing.
Post by Joel Mathew

and after saying all that, I wouldn’t make a separate model for the
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={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
*Sent:* Tuesday, November 27, 2018 12:24 PM
*Subject:* Unexpected behavior on delete of model
I have two Model classes, in two different apps which are part of the
same project. class doctor defined in appointments.models is a set of
attributes associated with a doctor, like name, username, email, phone etc.
class DoctorProfilePic is a Model defined in clinic.models, which has a
StdImageField which stores images. doctor has a One to One mapping to
DoctorProfilePic.
docid = models.AutoField(primary_key=True, unique=True) # Need
autoincrement, unique and primary
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
...
profilepic = models.ForeignKey(DoctorProfilePic, blank=True,
null=True, on_delete=models.CASCADE)
...
id = models.AutoField(primary_key=True, unique=True)
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),
})
doc = models.ForeignKey('appointments.doctor', blank=True,
null=True, on_delete=models.CASCADE)
When user selects one of the profile pics from a selection box, and
clicks Delete, django is supposed to delete the picture from the collection
of pictures uploaded by the doctor.
When the delete button is clicked, django deletes both the picture and
the doctor, instead of just the former.
docid = int(docid)
return HttpResponse("You dont have permissions to do this.")
doc = doctor.objects.get(docid = docid)
print(request.POST)
picid = int(request.POST.get('profilepic'))
print(f'doc:{doc} picid:{picid}')
pic = DoctorProfilePic.objects.get(doc = doc, id =picid)
pic.delete()
msg = f"Successfully removed profile picture."
msg = "Not a valid POST"
return HttpResponse(msg)
Now I'm guessing my problem is in defining the
on_delete=models.CASCADE? Can someone explain what I've done wrong?
Sincerely yours,
Joel G Mathew
--
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
<javascript:>.
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
<https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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
<javascript:>.
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
<https://groups.google.com/d/msgid/django-users/360df97b2ca14af69267bd6e98436ac6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>
.
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
<javascript:>.
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/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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/c330d2a3-3fa1-458f-9c4d-519c70de3257%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Joel Mathew
2018-11-28 07:39:21 UTC
Permalink
Yes, thank you, Todor.
Yesterday night, I ended up doing exactly this.
Sincerely yours,

Joel G Mathew
Post by Todor Velichkov
Right now, your doctor will be deleted if its ProfilePicture gets deleted.
I'm pretty sure you don't want to do that.
# 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)
# 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)
Post by Joel
Ignore the last post. Formatting blues in my main editor.
Post by Joel
I think you made a typo in the code. doctor can't be a Foreign key for
class Doctor. Anyway I get your point, and this is the same way I solved it
yesterday.
Post by Saurabh Agrawal
Post by Joel Mathew
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
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")
Setting on_delete to SET_NULL doesn't make much sense here, I think.
This implies that if a doctor is deleted, the profile pic will be
preserved, with the FK to doc being set to NULL in db (which doesn't seem
desirable). I think here CASCADE should work good and will not have the
issue that the OP is facing.
Post by Joel Mathew

and after saying all that, I wouldn’t make a separate model for the
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={
'large': (600, 400),
'thumbnail': (150, 140, True),
'medium': (300, 200),
})
*On Behalf Of *Joel Mathew
*Sent:* Tuesday, November 27, 2018 12:24 PM
*Subject:* Unexpected behavior on delete of model
I have two Model classes, in two different apps which are part of the
same project. class doctor defined in appointments.models is a set of
attributes associated with a doctor, like name, username, email, phone etc.
class DoctorProfilePic is a Model defined in clinic.models, which has a
StdImageField which stores images. doctor has a One to One mapping to
DoctorProfilePic.
docid = models.AutoField(primary_key=True, unique=True) # Need
autoincrement, unique and primary
name = models.CharField(max_length=35)
username = models.CharField(max_length=15)
...
profilepic = models.ForeignKey(DoctorProfilePic, blank=True,
null=True, on_delete=models.CASCADE)
...
id = models.AutoField(primary_key=True, unique=True)
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),
})
doc = models.ForeignKey('appointments.doctor', blank=True,
null=True, on_delete=models.CASCADE)
When user selects one of the profile pics from a selection box, and
clicks Delete, django is supposed to delete the picture from the collection
of pictures uploaded by the doctor.
When the delete button is clicked, django deletes both the picture and
the doctor, instead of just the former.
docid = int(docid)
return HttpResponse("You dont have permissions to do this.")
doc = doctor.objects.get(docid = docid)
print(request.POST)
picid = int(request.POST.get('profilepic'))
print(f'doc:{doc} picid:{picid}')
pic = DoctorProfilePic.objects.get(doc = doc, id =picid)
pic.delete()
msg = f"Successfully removed profile picture."
msg = "Not a valid POST"
return HttpResponse(msg)
Now I'm guessing my problem is in defining the
on_delete=models.CASCADE? Can someone explain what I've done wrong?
Sincerely yours,
Joel G Mathew
--
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
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
<https://groups.google.com/d/msgid/django-users/CAA%3Diw__vyDXxz6sfmcS_Bcy8M5cANmB7mwmTMwS4rfF96bJmbg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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
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
<https://groups.google.com/d/msgid/django-users/360df97b2ca14af69267bd6e98436ac6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>
.
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
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/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAL1UH0s7vMZ%3DWrU%2BJCwVMVzeQWkfAUM9p_KCgfhDQX7Nuv-GSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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
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/c330d2a3-3fa1-458f-9c4d-519c70de3257%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/c330d2a3-3fa1-458f-9c4d-519c70de3257%40googlegroups.com?utm_medium=email&utm_source=footer>
.
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/CAA%3Diw_9kRoMn3vc0RS_ZyvdjQp30uK6w9BcE-usrSnad-Kj_Cg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...