Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 无法分配";u''&引用;:&引用;“公司.母公司”;必须是一个";“公司”;实例_Django - Fatal编程技术网

Django 无法分配";u''&引用;:&引用;“公司.母公司”;必须是一个";“公司”;实例

Django 无法分配";u''&引用;:&引用;“公司.母公司”;必须是一个";“公司”;实例,django,Django,我每次尝试都会得到这个 无法分配“u”:“Company.parent”必须是“Company”实例。 我不知道还能做什么。 视图代码仍然不完整,对此表示抱歉。 我是否向表单传递了错误的参数 我有以下型号: models.py class Company(AL_Node): parent = models.ForeignKey('self', related_name='children_set',

我每次尝试都会得到这个

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

我有以下型号:

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()
以下表格为.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})

错误消息表示您正试图设置与字符串的关系,但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)

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

错误消息表示您正在尝试设置与字符串的关系,但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)

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

可能
new_company.parent=company.objects.get(id=my_company\u id)
而不是
new_company.parent=my_company\u id
@PauloScardine你应该把它作为答案发布:)做到了,它仍在生产同样的产品problem@smndak:确实用实例替换了所有使用id设置外键字段的实例吗?可能是
new\u company.parent=company.objects.get(id=my\u company\u id)
而不是
new\u company.parent=my\u company\u id
@PauloScardine您应该将其作为答案发布:)这样做了吗,它仍在生产同样的产品problem@smndak:是否确实已将使用id设置外键字段的所有实例替换为实例?