2012-07-15 14 views
18

Podążam za dokumentacją Django here w celu osiągnięcia prostego celu: Utwórz profil użytkownika natychmiast po utworzeniu nowego użytkownika.Django - Utwórz profil użytkownika podczas tworzenia użytkownika

Mam app 'kontach i moi accounts.models wygląda następująco:

# -*- coding: utf-8 -*- 
from django.db import models 
from django.db.models.signals import post_save 
from django.contrib.auth.models import User 
from main.models import Store 

class UserProfile(models.Model): 

    GENRE_CHOICES = (
     ('m', 'Masculino'), 
     ('f', 'Feminino'), 
    ) 
    MARITAL_STATUS_CHOICES = (
     ('s', 'Solteiro'), 
     ('c', 'Casado'), 
     ('d', 'Divorciado'), 
     ('v', 'Viúvo'), 
    ) 

    user = models.ForeignKey(User, unique=True) 
    birth_date = models.DateField() 
    genre = models.CharField(max_length=1, choices=GENRE_CHOICES) 
    address = models.CharField(max_length=150) 
    postal_code_4 = models.PositiveIntegerField() 
    postal_code_3 = models.PositiveIntegerField() 
    locatity = models.CharField(max_length=30) 
    marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES) 
    child_amount = models.PositiveSmallIntegerField() 
    is_merchant = models.BooleanField(default=False) 
    store = models.ForeignKey(Store, null=True) 

def create_user_profile(sender, instance, created, **kwargs): 
    if created: 
     UserProfile.objects.create(user=instance) 

post_save.connect(create_user_profile, sender=User) 

Wszystko wygląda w porządku dla mnie, ale podczas próby dodania nowego użytkownika (przy użyciu django admin), zamiast mieć nowo utworzony użytkownik i profilu użytkownika, pojawia się następujący błąd: Internalerror w/admin/auth/user/dodać/ obecna transakcja zostaje przerwana, polecenia ignorowane aż do końca bloku transakcji

Oto część błąd traceback :

/djangoProjects/lwboanova/lwboanova/apps/accounts/models.py in create_user_profile 

34: UserProfile.objects.create(user=instance) 

Wygląda na błąd integralności, ale nie rozumiem tego.

Byłoby wspaniale, gdyby ktoś z was mógł mi w tym pomóc.

+3

Czy masz zainstalowany pasek narzędzi Django? Jeśli tak, spróbuj go wyłączyć, aby uzyskać lepsze informacje o błędzie. –

Odpowiedz

14

Właśnie to wymyśliłem.

Zapomniałem dodać null=True do pozostałych pól modelu UserProfile.

Więc pola accounts.models.UserProfile teraz wygląda:

user = models.ForeignKey(User, unique=True) 
birth_date = models.DateField(null=True) 
genre = models.CharField(max_length=1, choices=GENRE_CHOICES, null=True) 
address = models.CharField(max_length=150, null=True) 
postal_code_4 = models.PositiveIntegerField(null=True) 
postal_code_3 = models.PositiveIntegerField(null=True) 
locatity = models.CharField(max_length=30, null=True) 
marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES, null=True) 
child_amount = models.PositiveSmallIntegerField(null=True) 
is_merchant = models.BooleanField(default=False) 
store = models.ForeignKey(Store, null=True) 

... i wszystko działa zgodnie z przeznaczeniem!

Cheers dla próbuje pomóc Ashray ^^

+0

, czy możesz sprawdzić i poprowadzić mnie, WIĘC pytanie http://stackoverflow.com/questions/17806683/create-row-of-date-while-creating-superuser – user2086641

14

Nie należy używać:

user = models.ForeignKey(User, unique=True) 

Zamiast korzystać z tego:

from django.conf import settings 
.. 
user = models.OneToOneField(settings.AUTH_USER_MODEL) 
+0

Masz rację co do OneToOneField, ale związek z model użytkownika jest w porządku. Oznacza to, że jeśli rozszerzysz kompilację w User o dodatkowe pola. Używaj tylko ustawienia 'AUTH_USER_MODEL', jeśli chcesz zastąpić kompilację w Modelu użytkownika swoim własnym. – allcaps

+3

@allcaps Ostatnie zdanie wprowadza w błąd. 'settings.AUTH_USER_MODEL' jest sposobem na ochronę tego fragmentu kodu przed błędem regresji, jeśli na stałe" User "nie jest już modelem w przyszłości. – JSmyth

0
def create_profile(sender,**kwargs): 
    if kwargs['created']: 
     user_profile=UserProfile.objects.create(user=kwargs['instance']) 


post_save.connect(create_profile,sender=User) 

Myślę, że to pomoże.

Powiązane problemy