アイテム.モデル.py
from django.db import models
class Slugged(models.Model):
title = models.CharField(max_length=500)
slug = models.CharField(max_length=2000, blank=True, null=True)
class Meta:
abstract = True
def __unicode__(self):
return self.title
class Item(Slugged):
# SET_NULL for safety
category = models.ForeignKey("Category", blank=True, null=True, on_delete=models.SET_NULL)
class Category(Slugged):
pass
items.views.py
from django.views import generic
from django.views.generic.edit import FormMixin
from jsonview.decorators import json_view
# this is from @jsocol/django-jsonview but there are other ways to do this
from .models import Item, Category
class CategoryUpdateView(generic.UpdateView):
model = Category
def get_context_data(self, **kwargs):
self.object = super(CategoryUpdateView, self).get_object()
context = super(CategoryUpdateView, self).get_context_data(**kwargs)
context['available_fks'] = Category.objects.exclude(pk=self.object.id)
context.update(kwargs)
return super(CategoryUpdateView, self).get_context_data(**kwargs)
class CategoryDeleteView(generic.DeleteView, FormMixin):
model = Task
success_url = '/'
@json_view
def dispatch(self, *args, **kwargs):
return super(CategoryDeleteView, self).dispatch(*args, **kwargs)
def delete(self, request, *args, **kwargs):
self.object = super(CategoryDeleteView, self).get_object()
self.object.delete()
return {'success':True}
def post(self, request, *args, **kwargs):
self.object = super(CategoryDeleteView, self).get_object()
if request.POST.has_key('new_fk'):
new_fk = get_object_or_404(Category, pk=request.POST['new_fk'])
for item in self.object.item_set.all():
item.category = new_fk
item.save()
else:
return {'success': False}
return self.delete(self, request, *args, **kwargs)
items.templates.items.category_update.html
<form action="" method="post" enctype="multipart/form-data" id="category-delete-form">
{% csrf_token %}
{{ form }}
<select id="new_fk">
<option value="" >---------</option>
{% for fk in available_fks %}
<option value="{{ fk.if }}"{% if forloop.first %} selected="selected"{% endif %}>{{ fk.title }}</option>
{% endfor %}
</select>
<input type="submit" name="delete_category" value="Delete">
</form>
items.static.items.deleteItem.js
function deleteCategory(deleteUrl, form_id) {
var $form = $('#' + form_id);
var new_pk = $form.find('#new_fk').val();
$.ajax({
url: deleteUrl,
type: 'POST',
data: 'csrfmiddlewaretoken=' + getCookie('csrftoken') + '&new_fk=' + new_fk,
success: function(data) {
if (data['success']){
$form.remove();
alert('success');
}
else {
alert('FAIL');
}
}
});
}
$('#category-delete-form').on('submit', function () {
$this = $(this);
deleteCategory($this.attr('action'), $this.id);
}
@goldhand要旨