2012-10-14 41 views
1

我在Ubuntu 12.04上使用Django 1.4和Python 2.7。Django ORM嘗試使用附加信息時給出錯誤

編輯:我正在清除這個問題的一些錯誤部分,因爲它似乎重現了同樣的問題。它的工作非常出色 - 我沒有改變 - 現在它失敗了。那是怎麼回事?

這基本上是有什麼看法是這樣的:

@login_required 
def request_new_project(request): 
    """ 
    .. function:: request_new_project() 

     Collect information to call the form used to create a new project 

     :param request: Django Request object 
    """ 
    user_dict = { 'rsb_username' : request.user.username} 
    form = CreateProject(initial = user_dict) 
    data = { 'user' : request.user } 
    data.update(csrf(request)) 
    data.update({ 'form' : form }) 

    return render_to_response("create_project.html", data) 

@login_required 
def add_project(request): 
    """ 
    .. function:: add_project() 

     Add a project for the user 

     :param request: Django Request object 
    """ 

    if (request.method == "POST"): 
     user = User.objects.get(username = request.POST.get('rsb_username')) 
     userProfile = UserProfile.objects.get(user = user) 

     new_project = Projects(client = userProfile, 
           project_name = request.POST.get('rsb_project_name'), 
           description = request.POST.get('rsb_description'), 
           budget = request.POST.get('rsb_budget'), 
           time_frame = request.POST.get('rsb_time_frame'), 
           time_frame_units = request.POST.get('rsb_time_frame_units'), 
           contact = request.POST.get('rsb_point_of_contact'), 
           contact_email = request.POST.get('rsb_contact_email'), 
           contact_phone = request.POST.get('rsb_contact_phone'), 
           price_quote = 0, 
           eta = 'To Be Determined', 
           current_status = 'Waiting for quote', 
           ) 
     new_project.save() 
     return view_projects(request) 

我得到以下錯誤:

Cannot assign "<UserProfile: UserProfile object>": "Projects.client" must be a "User" instance. 

我沒有改變的車型。

# Create a table for users 
class UserProfile(models.Model): 
    user = models.OneToOneField(User) 

    # Client Info 
    company_name = models.CharField(max_length = 200) 
    client_type = models.CharField(max_length = 200) 
    address1 = models.CharField(max_length = 200) 
    address2 = models.CharField(max_length = 200) 
    city = models.CharField(max_length = 200) 
    state = models.CharField(max_length = 200) 
    country = models.CharField(max_length = 200) 
    zip_code = models.CharField(max_length = 200) 
    phone_number = models.CharField(max_length = 200) 

# Create a table to manage project requests 
class Projects(models.Model): 
    client = models.ForeignKey(User) 
    project_name = models.CharField(max_length = 50) 
    description = models.TextField() 
    budget = models.CharField(max_length = 50) 
    time_frame = models.DecimalField(max_digits = 3, decimal_places = 1) 
    time_frame_units = models.CharField(max_length = 25) 
    contact = models.CharField(max_length = 50) 
    contact_email = models.EmailField() 
    contact_phone = models.CharField(max_length = 25) 
    price_quote = models.DecimalField(max_digits = 10, decimal_places = 2) 
    eta = models.CharField(max_length = 200) 
    current_status = models.CharField(max_length = 200) 

有什麼建議嗎?

更新1: 從實際的數據庫中,我可以看到的rsb_projects約束之一是: rsb_projects_client_id_fkey (client_id) REFERENCE rsb_userprofile (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED

如果幫助...

在我看來,即使我已經定義ForeignKey中的models.py是針對User的數據庫想要的UserProfile id。

想法?

回答

2

錯誤正是它所說的。

爲了創建你需要給你的項目新項目對象一個用戶配置文件對象,而不是用戶個人資料ID

user = User.objects.get(id=7) 
    userprofile = user.get_profile() 
    project.client = userprofile 

請注意,它是userprofile分配給項目實例的客戶端屬性的對象。您不能userprofile對象的id分配給項目實例的客戶端屬性。

此外,請勿將您的用戶標識與您的用戶配置文件ID混淆。他們可能不完全一樣。

用戶標識是在數據庫中創建新用戶時在auth_user表中自動生成的主鍵。 每當創建與用戶相關的相應用戶配置文件時,userprofile id是profiles_userprofile表中自動生成的主鍵。

換句話說,你add_project視圖功能需要讀取類似

if (request.method == "POST"): 
    user = User.objects.get(username = request.POST.get('rsb_username')) 
    # userprofile = UserProfile.objects.get(user = user) <-- we don't need this anymore. 

    new_project = Projects(client = user, # <--- give it a user instance of course now that your model has changed 
          project_name = request.POST.get('rsb_project_name'), 
          description = request.POST.get('rsb_description'), 
          budget = request.POST.get('rsb_budget'), 
          time_frame = request.POST.get('rsb_time_frame'), 
          time_frame_units = request.POST.get('rsb_time_frame_units'), 
          contact = request.POST.get('rsb_point_of_contact'), 
          contact_email = request.POST.get('rsb_contact_email'), 
          contact_phone = request.POST.get('rsb_contact_phone'), 
          ) 
    new_project.save() 

關鍵外賣是,你需要確保你的原始模型的定義是什麼。

如果在您的Project類中,您的客戶端屬性被分配爲FK給用戶,那麼您需要給它一個用戶對象。

如果您的項目類,你的客戶屬性被指定爲FK給用戶配置,那麼你需要給它一個USERPROFILE對象。

+0

在你的settings.py文件,指定 'AUTH_PROFILE_MODULE =「profiles.UserProfile'' 注意到,‘曲線’將是不管你命名你USERPROFILE應用是。如果您的用戶個人資料應用被稱爲userprofile,那麼它應該是 'AUTH_PROFILE_MODULE ='userprofile.UserProfile'' 指定此設置的目的在django文檔中描述https://docs.djangoproject.com/en/dev/ref/settings /#auth-profile-module –

+0

請忽略我以前的(現在刪除的)評論。它在你回答你的問題之前發佈。 – Rico

+0

很酷。讓我知道這是否能解決您的問題。乾杯。 :-) –

相關問題