【问题标题】:How am I saving with a null user ID in a Django view?如何在 Django 视图中使用空用户 ID 保存?
【发布时间】:2012-02-16 21:14:35
【问题描述】:

我有一个 Django 视图,它应该保存一个包含三个字段集的 CalendarEvent(模型实例),包括当前用户的配置文件对象。视图受@login_required 保护,我以超级用户身份登录,然后调用视图得到错误:

    /Users/jonathan/pim/../pim/pim_calendar/views.py in save_calendar
            date_padding = ''
        regexp = weekday + ' ' + month + ' ' + date_padding + date + \
          ' ..:..:.. ... ' + year
        event = pim_calendar.models.CalendarEvent()
        event.when = regexp
        event.description = description
        event.user = request.user.get_profile()
        event.save() 

在回溯中。 (request.user 应该是我以超级用户身份登录。)

我得到的错误是:

IntegrityError at /save_calendar
pim_calendar_calendarevent.userprofile_id may not be NULL
Request Method: POST
Request URL:    http://localhost:8000/save_calendar
Django Version: 1.3.1
Exception Type: IntegrityError
Exception Value:    
pim_calendar_calendarevent.userprofile_id may not be NULL
Exception Location: /usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 234
Python Executable:  /usr/local/bin/python
Python Version: 2.7.0
Python Path:    
['/Users/jonathan/pim',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/pip-0.8.1-py2.7.egg',
 '/usr/local/Cellar/python/2.7/lib/python27.zip',
 '/usr/local/Cellar/python/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-old',
 '/usr/local/Cellar/python/2.7/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/PIL',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time:    Thu, 16 Feb 2012 15:00:21 -0600

如果我在 event.save() 调用之前添加,

        print repr(event.user)

在 IntegrityError 崩溃之前,控制台会显示 """"""。

有什么建议吗?

【问题讨论】:

    标签: python django django-models django-views


    【解决方案1】:

    从错误消息看来,而不是:

        event.user = request.user.get_profile()
    

    你应该这样称呼:

        event.userprofile = request.user.get_profile()
    

    【讨论】:

      【解决方案2】:

      除了@jkbr 所说的之外,您可能还需要考虑不将 UserProfile 引用保存在 CalendarEvent 对象中。相反,只需保存与 CalendarEvent 关联的用户,然后通过以下方式访问 UserProfile:

      # Save event
      event = pim_calendar.models.CalendarEvent()
      event.user = request.user
      event.user.save()
      
      # Access the profile
      event = CalendarEvent.objects.get(...)
      the_userprofile = event.user.get_profile()
      

      【讨论】:

        猜你喜欢
        • 2011-01-14
        • 1970-01-01
        • 2017-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        相关资源
        最近更新 更多