0

我是 Pinax 和 Django 的新手。我试图通过从我插入的另一个应用程序(在本例中为 django-swingtime :http ://code.google.com/p/django-swingtime/ )中提取 OneToOneField 来扩展 Pinax Profile 模型。我的所有模型都显示在 django 管理界面中,但我无法添加新用户(我想在添加新配置文件的过程中这样做)。我收到以下错误:

IntegrityError at /admin/auth/user/add/

profiles_profile.event_type_id may not be NULL

Request Method:     POST
Request URL:    http://localhost:8000/admin/auth/user/add/
Django Version:     1.3.1
Exception Type:     IntegrityError
Exception Value:    

profiles_profile.event_type_id may not be NULL

我的 Pinax 版本是 0.9a2。EventType 是 django-swingtime 的一个模型。当我尝试从 Django 管理员中的任何位置添加用户时,我收到此错误。

这是我的 Profiles/models.py (更改的行旁边有注释)

from django.db import models
from django.utils.translation import ugettext_lazy as _

from idios.models import ProfileBase

from swingtime.models import EventType #ADDED HERE

class Profile(ProfileBase):
    name = models.CharField(_("name"), max_length=50, null=True, blank=True)
    about = models.TextField(_("about"), null=True, blank=True)
    location = models.CharField(_("location"), max_length=40, null=True, blank=True)
    website = models.URLField(_("website"), null=True, blank=True, verify_exists=False)
    event_type = models.OneToOneField(EventType) #ADDED HERE

    def __unicode__(self):
        return "Name: %s -- %s" % (self.name, self.about) #ADDED HERE

也许有人可以解释帐户、配置文件和用户之间的关系,以及哪些文件可以编辑,哪些文件不宜编辑(例如,我认为我不想更改我的 Pinax 站点包中的任何内容。 ..),我可以取得一些进展。另外,我假设这个 idios 插件参与了这个过程,但是我找到的文档链接会加载(http://oss.eldarion.com/idios/docs/0.1/)。

谢谢!

4

1 回答 1

0

I've solved my error, though I'm interested in other answers and additional information since some of this is speculation/still unclear to me. I think the error stems from Pinax accounts automatically creating a "blank" idios profile for every new user created (speculation). Since I had said every profile must have a OneToOne foreign field associated with it and had not allowed for this OneToOne field to be null, there was a problem.

This question describes some of the differences between the idios profile app, Pinax account and the standard django User accounts:

Difference between pinax.apps.accounts, idios profiles, and django.auth.User

What I did to solve this problem was using the Django's signalling capabilities to make sure that as soon as a Profile is generated, so is an instance of the foreign field. This is described here:

https://docs.djangoproject.com/en/1.3/topics/signals/

Make sure to read the bit about double signalling, since this has caused trouble for some other people:

https://docs.djangoproject.com/en/1.3/topics/signals/#preventing-duplicate-signals

The final modification to my code that got rid of the error was this. Note that besides adding signalling I also explicitly said that the OnetoOneField was allowed to be null.

from django.db import models
from django.utils.translation import ugettext_lazy as _
from idios.models import ProfileBase
from swingtime.models import EventType

#for signals
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(ProfileBase):
    name = models.CharField(_("name"), max_length=50, null=True, blank=True)
    about = models.TextField(_("about"), null=True, blank=True)
    event_type = models.OneToOneField(EventType, null=True)
    def __unicode__(self):
        return "Name: %s -- %s" % (self.name, self.about)

def create_User_EventType(sender, instance, created, **kwargs):
    print "checking creation of profile"
    if created:
        print "User event type is being created"
        event_label = "%s_hours" % (instance.name)
        print "the event label is" + event_label
        EventType.objects.create(abbr=instance.name,label=event_label)

post_save.connect(create_User_EventType,sender=Profile,dispatch_uid="event_post_save_for_profile")
于 2012-12-28T11:57:07.957 回答