Skip to content

Commit b086d4c

Browse files
committed
fix: Only show actual public objects as reason for failed deletion
1 parent b8b48e5 commit b086d4c

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

djangocms_versioning/models.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@
3636

3737

3838
def PROTECT_IF_PUBLIC_VERSION(collector, field, sub_objs, using):
39-
state = sub_objs[0].state # State of the first version in sub_objs
40-
if state == constants.PUBLISHED:
41-
models.PROTECT(collector, field, sub_objs, using)
39+
public_exists = sub_objs.filter(state=constants.PUBLISHED)
40+
if public_exists:
41+
# Any public objects?
42+
raise models.ProtectedError(
43+
f"Cannot delete some instances of model '{field.remote_field.model.__name__}' because they are "
44+
f"referenced through a protected foreign key: '{sub_objs[0].__class__.__name__}.{field.name}'",
45+
public_exists,
46+
)
4247
models.SET_NULL(collector, field, sub_objs, using)
4348

4449

@@ -142,7 +147,14 @@ class Meta:
142147
)
143148

144149
def __str__(self):
145-
return f"Version #{self.pk}"
150+
state = dict(constants.VERSION_STATES).get(self.state, self.state)
151+
if self.object_id:
152+
try:
153+
return f"Version #{self.pk} ({state}) of {self.content}"
154+
except Exception:
155+
# In case the content object cannot be stringified
156+
pass
157+
return f"Version #{self.pk} ({state})"
146158

147159
def verbose_name(self):
148160
return _("Version #{number} ({state} {date})").format(

0 commit comments

Comments
 (0)