0

Pinax と Django は初めてです。プラグインした別のアプリケーション (この場合は django-swingtime : http://code.google.com/p/django-swingtime/ )からプルする OneToOneField を使用して、Pinax プロファイル モデルを拡張しようとしています。すべてのモデルを 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 admin 内の任意の場所からユーザーを追加しようとすると、このエラーが発生します。

これが私の 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

エラーを解決しましたが、他の回答や追加情報に興味があります。これは推測であり、まだ不明であるためです。このエラーは、作成された新しいユーザーごとに「空白の」idios プロファイルを自動的に作成する Pinax アカウントに起因すると考えられます (推測)。すべてのプロファイルには OneToOne 外部フィールドが関連付けられている必要があり、この OneToOne フィールドが null になることを許可していなかったので、問題が発生しました。

この質問では、idios プロファイル アプリ、Pinax アカウント、および標準の django ユーザー アカウントのいくつかの違いについて説明します。

pinax.apps.accounts、idios プロファイル、および 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 を 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 に答える