1

我每次尝试都会得到这个。

无法分配“u”:“Company.parent”必须是“Company”实例。我不知道还能做什么。视图代码仍然是半生不熟,抱歉。我是否向表单传递了错误的参数?

我有以下模型:

模型.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})
4

1 回答 1

1

错误消息说您正在尝试设置与字符串的关系,但 Django 期望该值是 Company 模型的实例。您应该为外键字段分配一个真实的模型实例,而不仅仅是主键。

我在代码中发现了您分配 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)

这将节省访问数据库的时间。

于 2013-08-04T17:53:45.107 回答