MultipleObjectsReturned - get()返回多个ContentType - 它返回2

时间:2017-04-27 08:37:23

标签: django django-models django-views django-contenttypes

一切正常,然后我突然得到了这个错误

  

MultipleObjectsReturned at / rohit / post / new-post /

     

get()返回多个ContentType - 它返回2!

我不知道为什么它会返回2个对象。它只是假设返回一个,我不能 想办法。帮帮我们这是我的代码:

models.py:

class Comment(models.Model):
    user        = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    parent      = models.ForeignKey("self", null=True, blank=True)

    content     = models.TextField()
    timestamp   = models.DateTimeField(auto_now_add=True)

    objects = CommentManager()

    class Meta:
        ordering = ['timestamp']


    def __unicode__(self):  
        return str(self.user.username)

    def __str__(self):
        return str(self.user.username)

    def get_absolute_url(self):
        return reverse("comments:thread", kwargs={"id": self.id})

    def get_delete_url(self):
        return reverse("comments:delete", kwargs={"id": self.id})

    def children(self): #replies
        return Comment.objects.filter(parent=self)

    @property
    def is_parent(self):
        if self.parent is not None:
            return False
        return True

Views.py:

instance = get_object_or_404(Post, slug = slug)
initial_data = {
            "content_type": instance.get_content_type,
            "object_id": instance.id
    }
    print(initial_data)
    form = CommentForm(request.POST or None, initial= initial_data)
    if form.is_valid() and request.user.is_authenticated():
        c_type = form.cleaned_data.get("content_type")
        print('c_type : ',c_type)
        content_type = ContentType.objects.get(model=c_type)
        print(content_type)
        obj_id = form.cleaned_data.get('object_id')
        content_data = form.cleaned_data.get("content")
        parent_obj = None
        try:
            parent_id = int(request.POST.get("parent_id"))
        except:
            parent_id = None

        if parent_id:
            parent_qs = Comment.objects.filter(id=parent_id)
            if parent_qs.exists() and parent_qs.count() == 1:
                parent_obj = parent_qs.first()


        new_comment, created = Comment.objects.get_or_create(
                            user = request.user,
                            content_type= content_type,
                            object_id = obj_id,
                            content = content_data,
                            parent = parent_obj,
                        )

Forms.py:

class CommentForm(forms.Form):
    content_type = forms.CharField(widget=forms.HiddenInput)
    object_id = forms.IntegerField(widget=forms.HiddenInput)
    #parent_id = forms.IntegerField(widget=forms.HiddenInput, required=False)
    content = forms.CharField(label='',widget=forms.Textarea(attrs={'class':"mdl-textfield__input",'type':"text", 'rows':"3", 'id':"sample5"}))

2 个答案:

答案 0 :(得分:1)

我刚遇到这个问题。

在您的数据库中,有一个名为 django_content_type 的表。

因为你有一些旧的content_type具有相同的名称。

您可以使用 sqlitebrowser 删除这些内容。

答案 1 :(得分:-1)

如果你想调试,我建议你使用filter而不是get。

在您看来:

content_type = ContentType.objects.filter(model=c_type)
print(content_type)

现在在content_type中,您将拥有2个对象。