Django 无法分配";“42”字:&引用;事件。用户“U id”;必须是一个";“用户”;实例

Django 无法分配";“42”字:&引用;事件。用户“U id”;必须是一个";“用户”;实例,django,python-2.7,Django,Python 2.7,我已经检查了所有与我的问题相关的解决方案,但是没有人工作,我有一个事件表,在其中我分配了用户id。事件模型是 class Event(models.Model): user_id=models.ForeignKey(User, on_delete=models.CASCADE) event_auth_id=models.CharField(null=True, max_length=225) event_title=models.CharField(max_length=

我已经检查了所有与我的问题相关的解决方案,但是没有人工作,我有一个事件表,在其中我分配了用户id。事件模型是

class Event(models.Model):
    user_id=models.ForeignKey(User, on_delete=models.CASCADE)
    event_auth_id=models.CharField(null=True, max_length=225)
    event_title=models.CharField(max_length=225)
    ticket_title=models.CharField(max_length=225)
    category=models.CharField(max_length=50)
    event_summary=models.TextField()
    event_information=models.TextField()
    restriction=models.CharField(max_length=50, default='No Restriction')
    artist_image=models.CharField(null=True, max_length=50)
    event_poster=models.CharField(null=True, max_length=50)
    notification_email=models.CharField(null=True, max_length=50)
    notification_frequency=models.CharField(null=True, max_length=15)
    name_on_ticket=models.CharField(max_length=225)
    event_tnc=models.TextField()
    current_step=models.IntegerField(null=True)
    event_status=models.BooleanField(default=True)
    created=models.DateTimeField(auto_now=True)
    modified=models.DateTimeField(auto_now_add=True)
我正在从视图分配登录的用户id

def create_new_event(request, steps):
if request.method == 'POST':
    if(steps=="step_1"):
        stepFirstForm = CreateEventStepFirstForm(request.POST)
        if stepFirstForm.is_valid():
            eventStepFirst = Event(
                user_id = request.user.id,
                event_auth_id = uuid4(),
                event_title = request.POST['event_title'],
                ticket_title = request.POST['ticket_title'],
                category = request.POST['categories'],
                event_summary = request.POST['event_summary'],
                event_information = request.POST['event_information'],
                restriction = request.POST['restrictions'],
                notification_email = request.POST['notification_email'],
                notification_frequency = request.POST['email_frequency']
            )
但这给了我错误

Cannot assign "42": "Event.user_id" must be a "User" instance.    

ForeignKey字段需要的是User类型的对象,而不是用户ID。请尝试将赋值
User\u ID=request.User.ID
更改为
User\u ID=request.User
。将字段重命名为“user”也可能有意义,以避免将来出现混淆。

问题在于以下代码:

    def create_new_event(request, steps):
    if request.method == 'POST':
        if(steps=="step_1"):
            stepFirstForm = CreateEventStepFirstForm(request.POST)
            if stepFirstForm.is_valid():
                eventStepFirst = Event(
                    user_id = request.user.id,
                    event_auth_id = uuid4(),
                    event_title = request.POST['event_title'],
                    ticket_title = request.POST['ticket_title'],
                    category = request.POST['categories'],
                    event_summary = request.POST['event_summary'],
                    event_information = request.POST['event_information'],
                    restriction = request.POST['restrictions'],
                    notification_email = request.POST['notification_email'],
                    notification_frequency = request.POST['email_frequency']
                )
在“user\u id=request.user.id”的位置,您应该使用“user\u id=request.user”或“user\u id=request.user.username”,因为在字段中

  user_id=models.ForeignKey(User, on_delete=models.CASCADE)
对于事件模型,您将user\u id指定为用户模型的外键,因此user\u id字段需要的是用户实例,而不是request.user.id


谢谢。

你需要
user\u id=request.user
我在做request.user.id,我不知道你只需要做request.user。非常感谢。