Reputation: 1460
I have a couple of models, Content
and Hostname
. Hostname
has a content_id
associating the models:
class Content(models.Model):
name = models.CharField(max_length=120)
message = models.TextField()
class Hostname(models.Model):
"""
Hostname model
"""
name = models.CharField(max_length=250)
content = models.ForeignKey(Content, on_delete=models.DO_NOTHING, null=True)
On the content delete form/view, I have a drop-down selector to specify what content should be assigned to hostnames that are associated with the content being deleted.
Contents:
Hostnames
Action:
The template for the content_confirm_delete
looks like this, when selecting the content_id
:
<select name="content_id">
{% for new_content in contents %}
{% if new_content.id != content.id %}
<option value="{{ new_content.id }}" {% if default_content == new_content.name %}selected{% endif %}>{{ new_content.name }}</option>
{% endif %}
{% endfor %}
</select>
The content delete is using a generic.DeleteView
:
class ContentsDeleteView(generic.DeleteView):
model = Content
success_url = reverse_lazy('content.list')
Upvotes: 1
Views: 319
Reputation: 1460
I've thus far gone with this:
class ContentsDeleteView(generic.DeleteView):
model = Content
success_url = reverse_lazy('content.list')
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
default_content_id = Setting.objects.get(name='default-content').value
Hostname.objects.select_related().filter(content_id=self.object.id).update(content_id=request.POST.get('content_id', default_content_id))
return super(ContentsDeleteView, self).delete(request, *args, **kwargs)
Seems to be working, but not sure if this is the "right" way according to Django.
Upvotes: 1