2013-08-03 70 views
1

我每次嘗試都會收到此消息。無法分配「u」'「:」Company.parent「必須是」公司「實例

無法分配「u」'「:」Company.parent「必須是」公司「實例。 我不知道還有什麼要做。 視圖代碼仍然是一半,對此抱歉。 我是否將錯誤的參數傳遞給窗體?

我有以下模式:

models.py

class Company(AL_Node): 
    parent = models.ForeignKey('self', 
          related_name='children_set', 
          null=True, 
          db_index=True) 
    node_order_by = ['id', 'company_name'] 
    id = models.AutoField(primary_key=True) 
    company_name = models.CharField(max_length=100L, db_column='company_name') # Field name made lowercase. 
    next_billing_date = models.DateTimeField() 
    last_billing_date = models.DateTimeField(null=True) 
    weekly = 'we' 
    twice_a_month = '2m' 
    every_two_weeks = '2w' 
    monthly = 'mo' 
    billing_period_choices = (
    (weekly, 'Weekly'), 
    (every_two_weeks, 'Every two weeks'), 
    (twice_a_month, 'Every two weeks'), 
    (monthly, 'Monthly'), 
) 
    billing_period = models.CharField(max_length=2, 
            choices=billing_period_choices, 
            default=weekly) 

    objects = CompanyManager() 

以下forms.py:

class newCompany(ModelForm): 
    company_name = forms.CharField(label='Company Name', 
           widget=forms.TextInput(attrs={'class': 'oversize expand input-text'})) 
    billing_period = forms.ModelChoiceField 
    next_billing_date = forms.CharField(widget=forms.TextInput(attrs={'class': 'input-text small', 'id': 'datepicker'})) 
    parent = forms.CharField(widget=forms.HiddenInput(), required=False) 

    class Meta: 
    model = Company 
    fields = ["company_name", "parent", "billing_period", "next_billing_date"] 

以下觀點:

def create_company(request): 
    userid = User.objects.get(username=request.user).id 
    my_company_id = CompanyUsers.objects.get(user_id=userid).company_id 
    my_company_name = Company.objects.get(id=my_company_id).company_name 
    machines = Title.objects.raw(
    'select machines.id, title.name, machines.moneyin, machines.moneyout, moneyin - moneyout as profit, machines.lastmoneyinoutupdate, (select auth_user.username from auth_user where machines.operator = auth_user.id) as operator, (select auth_user.username from auth_user where machines.readers = auth_user.id) as readers from machines, title where machines.title = title.id and machines.company_id =%s', 
    [my_company_id]) 
    if request.method == 'POST': 
    form_company = newCompany(request.POST) 
    if form_company.is_valid(): 
     new_company = form_company.save(commit=False) 
     new_company.parent = my_company_id 
     if request.POST.get('select_machine'): 
     selected_machine = request.POST.getlist('select_machine') 
     percentage = request.POST.get('percentage') 
     if not Beneficiary.objects.check_assign_machine(my_company_id, selected_machine, percentage): 
      target_company_name = new_company.company_name 
      target_company_id = Company.objects.get(company_name=target_company_name).id 
      new_company.save() 
      Machines.objects.assign_machine(target_company_id, selected_machine) 
      Beneficiary.objects.create_beneficiary(percentage, target_company_name, my_company_id, selected_machine) 
     else: 
      invalid_machines = Beneficiary.objects.check_assign_machine(my_company_id, selected_machine, percentage) 
      return render(request, 'lhmes/createcompany.html', 
       {'form_company': form_company, 'machines': machines, 'my_company_name': my_company_name, 'invalid_machines' : invalid_machines}) 
     else: 
     new_company.save() 

    else: 
    form_company = newCompany() 

    return render(request, 'lhmes/createcompany.html', 
       {'form_company': form_company, 'machines': machines, 'my_company_name': my_company_name}) 
+2

可能是'new_company.parent = Company.objects.get(id = my_company_id)'而不是'new_company.parent = my_company_id' –

+0

@PauloScardine您應該將其作爲回答:) –

+0

做到了這一點,它仍然生產相同問題 – smndak

回答

1

誤差消息說你是tr ying與字符串建立關係,但Django希望該值成爲公司模型的一個實例。您應該爲外鍵字段指定一個真實模型實例,而不是隻有主鍵。

我發現代碼中的幾個地方,你都分配一個PK:

new_company.parent = my_company_id 

在機型希望它是一個實例:

new_company.parent = Company.objects.get(id=my_company_id) 

我真的不請記住這是否可行,但您可以嘗試:

new_company.parent_id = int(my_company_id) 

這樣可以省去一趟數據庫。

相關問題