2015-04-17 147 views
0

我需要我的django用戶屬於Office。Django - 將數據附加到不會保存到數據庫的模型

我做了以下擴展默認的用戶模式:

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    office = models.ForeignKey('Office') 

用戶模型我post_save信號調用這個函數:

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

我基本上要附加辦公室「實例」 。

user = User(
     office = office, 
     username = username, 
     email = email, 
     password = password 
    ) 

但我得到的錯誤:

'office' is an invalid keyword argument for this function

這是正常的,因爲辦公室是不是爲用戶模型的字段,當我創建我做了以下的用戶,這意味着。 但是有沒有辦法告訴Django忽略這個字段,這個字段只是在這裏暫時「攜帶」信息?

回答

1

只需設置office作爲一個簡單的屬性:

user = User(username=username, email=email) 
user.set_password(password) 
user.office = office 
user.save()