2011-12-11 30 views
2

我想延長Django的auth.User,閱讀this問題,
這是我的models.py後:爲什麼沒有auth.User檢查必填字段?

from django.db import models 
from django.contrib.auth.models import User 
from django.db.models.signals import post_save 

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    age = models.SmallIntegerField() 

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

post_save.connect(create_user_profile, sender=User) 

和我在settings.py的底部加入這一行:

AUTH_PROFILE_MODULE = 'MYAPP.UserProfile' 

的問題是,當我運行python manage.py shell,和類型:

from django.contrib.auth.models import User 
user = User() 

它的工作沒有問題!爲什麼它不給我一個錯誤,我沒有給username/password

回答

0

請確保UserProfile位於MYAPP的models.py中,並且MYAPP在INSTALLED_APPS中註冊。看起來你的信號根本不起作用。如果它不起作用,請嘗試在shell中:

from django.contrib.auth.models import User 
from MYAPP.models import UserProfile 
user = User() 

因此,您將確保該信號已正確註冊。

+0

當我在django shell中鍵入這3行代碼時,它沒有錯誤地工作 – wong2

6

當您運行user = User(),你正在做的是創造一個新的用戶實例。在您嘗試使用user.save()進行保存之前,它不會拋出錯誤。

要同時創建和保存一個新的模型實例到DB:

user = User.objects.create() # should throw an IntegrityError due to required (not NULL) fields not provided 
+0

看來,在我鍵入user.save()之前,新用戶已經出現在數據庫和Django的管理員....我嘗試鍵入user.save(),它的工作原理沒有任何錯誤 – wong2

+0

新用戶沒有出現在數據庫中,直到你做user.save()。在Django文檔中查看此解釋[https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model]。如果您查看用戶模型的模型定義,則沒有任何字段具有'null = True',因此對於CharField'字段(如用戶名和密碼),在保存用戶創建的用戶實例時使用空字符串= User()'。所有其他字段都有默認值,因此'用戶'對象實際上會生成一個有效的數據庫條目。 –

+0

@SimonKagwi是對的,你(@ wong2)是錯的(對不起)。然後使用'my_user.get_profile()'訪問'Profile'模型。 – Stan

0

的限制主要是針對表單驗證;只有很少的限制(主要是那些可能導致數據庫問題的限制),這些限制在您使用API​​時實施。

您可以輕鬆創建一個空的用戶(沒有用戶名的用戶):

>>> from django.contrib.auth.models import User 
>>> u = User() 
>>> u.save() 
>>> u 
<User: > 
>>> u.id 
2 

但是,如果您嘗試創建空的用戶,你會得到一個IntegrityError

>>> u = User() 
>>> u.save() 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save 
    self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/models/base.py", line 553, in save_base 
    result = manager._insert(values, return_id=update_pk, using=using) 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/models/manager.py", line 195, in _insert 
    return insert_query(self.model, values, **kwargs) 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/models/query.py", line 1436, in insert_query 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 791, in execute_sql 
    cursor = super(SQLInsertCompiler, self).execute_sql(None) 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute 
    return self.cursor.execute(sql, params) 
    File "/Users/burhan/work/projects/foo/env/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute 
    return Database.Cursor.execute(self, query, params) 
IntegrityError: column username is not unique 

如果你看看模型,你會看到username字段有一個db級限制(unique=True):

`username = models.CharField(_('username'), max_length=30, unique=True...` 

這在API級別強制執行,所以你不能有兩個用戶使用相同的username領域。

的另一個例子是選擇參數。這主要用於演示。如果你有一個字段choices=('M','Male'),('F','Female');使用API​​,你可以插入任何單個字符,它會愉快地接受它。那些在數據庫級別執行

選項(這意味着,你無法從API 「違反」 他們)是:

  1. unique
  2. max_length
  3. null(不blank混淆)
相關問題