2012-12-24 15 views
0

我是Pinax和Django的新手。我試圖通過從插入的其他應用程序(在本例中,django-swingtime:http://code.google.com/p/django-swingtime/)提取OneToOneField來擴展Pinax Profile模型。我已經將所有模型顯示在django管理界面中,但我無法添加新用戶(我想在添加新配置文件的過程中執行此操作)。我收到以下錯誤:在Pinax中自定義配置文件模型:添加OnetoOneField時在管理中出現完整性錯誤

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管理員中的任何地方添加用戶時,出現此錯誤。

這裏是我的個人資料/ 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/)。

謝謝!

回答

0

我已經解決了我的錯誤,但我對其他答案和其他信息感興趣,因爲其中一些是猜測/我還不清楚。我認爲這個錯誤源於Pinax帳戶,它會爲每個新創建的用戶(推測)自動創建一個「空白」idios配置文件。因爲我曾經說過每個配置文件都必須有一個OneToOne外部字段,並且不允許此OneToOne字段爲空,所以出現問題。

這個問題介紹了一些idios輪廓應用,Pinax帳戶和標準的Django用戶帳戶之間的區別:

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

我做了什麼來解決這個問題是使用Django的信令功能,使確保一旦生成配置文件,外部字段的實例也是如此。這說明如下:

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

請務必閱讀約一倍信令位,因爲這已經引起麻煩一些人:

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

最後修改我的代碼,擺脫了這個錯誤。請注意,除了添加信號之外,我還明確表示OnetoOneField被允許爲空。

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") 
相關問題