2013-07-30 44 views
1

我的觀點:Django的1.5.1錯誤DoesNotExist:爲MyModel匹配查詢不存在,但存在

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)

思想如預期? 它是否正確的工作流程?

+1

你能證明你的'MyModel'? – karthikr

+0

我要更新這個問題。 – Daviddd

+0

你在哪一行得到錯誤? –

回答

4

我不明白爲什麼評論該行將使工作,但你無論如何都應該趕上錯誤:

try: 
    id_osirix = int(request.POST.get('id')) 
except ValueError: 
    return HttpResponseBadRequest(_('ID is not numeric')) 

try: 
    osirix = Osirix.objects.get(pk=id_osirix) 
except Osirix.DoesNotExist: # catch the DoesNotExist error 
    osirix = None 

if osirix: # delete if only exists 
    url = osirix.url 
    osirix.delete() 
+0

這是否完全消除了這個問題? –

+0

我刪除了數據庫表,然後運行syncdb --migrate,問題消失了。 – Daviddd

+0

好的,你解決了你的問題。 –

相關問題