私の見解:
try:
id_osirix = int(request.POST.get('id'))
except ValueError:
return HttpResponseBadRequest(_('ID is not numeric'))
instance = Osirix.objects.get(pk=id_osirix)
url = instance.url
instance.delete()
# Reload MyModel code using url var
しかし、次のエラーがあります: DoesNotExist: MyModel に一致するクエリが存在しません。ルックアップ パラメータは {u'id__exact': 3} でした
コメントしたら
# url = instance.url
それは機能し、レコードを削除します。
MyModel クラスの定義
# base/models.py
from django.db import models
class TimeStampedModel(models.Model):
"""
An abstract base class model that provides selfupdating
created and modified fields.
"""
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
# apps/myapp/models.py
class OsirixType(models.Model):
name = models.CharField(max_length=255, help_text=_('Standards list'))
description = models.TextField(blank=True)
class Meta:
verbose_name = _("Osirix type")
verbose_name_plural = _("OsirixType multiple types")
# On Python 3: def __str__(self):
def __unicode__(self):
return self.name
# apps/myapp/models.py
class Osirix(TimeStampedModel):
"""
"""
owner = models.ForeignKey(User)
type = models.ForeignKey(OsirixType)
title = models.CharField(max_length=255)
keywords = TaggableManager(_('Keywords'), blank=True, help_text=_('Commonly used word(s)'))
operations = models.TextField(_('Operations'), blank=True)
url = models.URLField(verbose_name=_("URL"))
version = models.CharField(
max_length=20,
default='0.0.0',)
class Meta:
verbose_name = _("Osirix connection")
verbose_name_plural = _("Osirix multiple connections")
# On Python 3: def __str__(self):
def __unicode__(self):
return self.title
@property
def ttd(self):
from apps.fm.layers.models import Layer
return str(Layer.objects.filter(Osirix=self.id).aggregate(Avg('ttd'))['ttd__avg'])
def keyword_list(self):
keywords_qs = self.keywords.all()
if keywords_qs:
return ','.join([kw.name for kw in keywords_qs])
else:
return ''
@receiver(post_delete, sender=Osirix)
def osirix_post_delete_handler(sender, **kwargs):
osirix = kwargs['instance']
osirix = os.path.join(THUMBNAILS_ROOT, str(osirix.pk // DATA_PARTITION), str(osirix.pk)) + '/'
shutil.rmtree(osirix_thumbnail_root, ignore_errors=True)
前の行 (url = instance.url) をコメントすると、期待どおりに動作します。
アイデア?ワークフローは正しいですか?