Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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
Python __str_uu;返回的非字符串(类型Leads)_Python_Django - Fatal编程技术网

Python __str_uu;返回的非字符串(类型Leads)

Python __str_uu;返回的非字符串(类型Leads),python,django,Python,Django,我遇到以下错误,但我无法解决: TypeError:str返回的非字符串(类型Lead) Views.py class PpboxsolFormView(CreateView): model = PPboxSol form_class = Ppboxsolform template_name = 'pfep/PpboxsolForm.html' def get_initial(self): initial = super().get_initia

我遇到以下错误,但我无法解决:

TypeError:str返回的非字符串(类型Lead)

Views.py

class PpboxsolFormView(CreateView):
    model = PPboxSol
    form_class = Ppboxsolform
    template_name = 'pfep/PpboxsolForm.html'

    def get_initial(self):
        initial = super().get_initial()
        initial['pfep'] = Pfep.objects.get(pk=self.kwargs['pk'])
        return initial

    def form_valid (self, form):
        if self.request.user.employee.employee_role == 'admin':
            product = form.save(commit=False)
            product.created_by = Employee.objects.filter(user=self.request.user.id)[0]
            product.save()
            messages.success(self.request, 'The PP Box Solution was created with success!')
            return redirect('emp:ppbox_table')
        else:
            messages.success(self.request, "You don't have permission to create Solution!")
            return redirect('emp:ppbox_table')

        return redirect('emp:pfep_table')
url.py

     path('ppboxsol/add/<int:pk>/', PpboxsolFormView.as_view(), name='ppbox_form'),
path('ppboxsol/add/',PpboxsolFormView.as_view(),name='ppbox_form'),
我是否也需要将
def\uu str\uuuu(self):
添加到模型
PPboxSol

编辑:


我还添加了
潜在客户
模型,但这也会返回一个str

添加一个潜在客户字段名,如果我们想显示用户名,那么我们需要使用
user.username

试试这个:

class Pfep(models.Model):

    client = models.ForeignKey(Leads, on_delete=models.CASCADE, related_name='vendor_owner')
    receiver = models.CharField(max_length=250, default=0 ,blank=True, null=True)
    receiver_location = models.CharField(max_length=250, default=0 ,blank=True, null=True)
    def __str__(self):
        return str(self.client.name)#name for example

请出示您的潜在客户模型a
模型。ForeignKey
不是字符串。也许先把它扔给弦?它的意思就是它说的
TypeError
表示有错误,因为某些内容的类型不正确。它告诉您
\uuuu str\uuuu
方法返回了非字符串的内容。因此,返回的对象是错误类型的对象。您需要返回一个字符串。我知道问题出在哪里。。感谢您
def\uuu str\uuuu(self):返回self。客户机
返回整数,我试图将其作为str传递
class Pfep(models.Model):

    client = models.ForeignKey(Leads, on_delete=models.CASCADE, related_name='vendor_owner')
    receiver = models.CharField(max_length=250, default=0 ,blank=True, null=True)
    receiver_location = models.CharField(max_length=250, default=0 ,blank=True, null=True)
    def __str__(self):
        return str(self.client.name)#name for example