/ posts / create / 15 /处的ValueError:无法分配“< Group:Group 2>”:“ Post.group”必须是“ Group”实例 [英] ValueError at /posts/create/15/: Cannot assign "<Group: Group 2>": "Post.group" must be a "Group" instance

查看:59
本文介绍了/ posts / create / 15 /处的ValueError:无法分配“< Group:Group 2>”:“ Post.group”必须是“ Group”实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写帖子的create方法,该帖子属于一个组和一个用户。尽管我在文档和我查看过的所有其他来源中都是这样做的,但我一直收到此错误。这是相关的文件:

I am writing the create method for a post, which belongs to a group and a user. I keep getting this error, although this is how it's done in the documentation and every other source i've looked at. here are the relevant files:

models.py

models.py

from django.db import models
from django.contrib.auth.models import User, Group


class Post(models.Model):
    title = models.CharField(max_length=255, unique=True)
    body = models.TextField()
    likes_total = models.IntegerField(default=0)
    pub_date = models.DateTimeField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)


def __str__(self):
    return self.name

def summary(self):
    # return the 1st 100 chars
    return self.body[:100]

def pub_date_pretty(self):
    # strftime is how to break down time
    return self.pub_date.strftime('%b %e %Y')

posts.urls.py

posts.urls.py

from . import views
from django.urls import path


app_name = 'posts'

urlpatterns = [
    path('create/<int:group_id>/', views.create, name='create'),
]

posts.views.py

posts.views.py

def create(request, group_id):
    group = get_object_or_404(Group, pk= group_id)
    if request.method == 'POST':
        if request.POST['body']:
            post = Post()
            post.title = request.POST['title']
            post.body = request.POST['body']
            post.pub_date = timezone.datetime.now()
            post.author = request.user
            post.group = group
            post.save()
            return redirect('/groups/' + str(group_id))

        else:
            return render(request, 'groups/detail.html', {'group':group})

    else:
        return render(request, 'groups/detail.html', {'group':group})

将组保存到帖子(post.group = group)时会引发错误,尽管这是我学到的方法。希望有人能解决这个问题。

the error is thrown when I am saving the group to the post (post.group = group), although this the method that ive learned. Hope someone can figure this out. thanks!!

推荐答案

您似乎混淆了两个不同的 Group 楷模。在您的模型中,您有:

You seem to have mixed up two different Group models. In your model you have:

from django.contrib.auth.models import User, Group

,并且您认为:

from groups.models import Group 

这些是完全不同的类,这就是为什么会出现错误的原因告诉您不能将 groups.models.Group 分配给 auth.models.Group

These are completely different classes, which is why you get an error telling you that you can't assign groups.models.Group to what should be a auth.models.Group.

我不确定您打算使用哪个类,但是您需要在两个地方使用相同的类。

I'm not sure which you intend it to be, but you need to use the same class in both places.

这篇关于/ posts / create / 15 /处的ValueError:无法分配“&lt; Group:Group 2&gt;”:“ Post.group”必须是“ Group”实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆