Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.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 在Django Autocomplete灯光中使用自定义输出_Python_Django_Django Autocomplete Light - Fatal编程技术网

Python 在Django Autocomplete灯光中使用自定义输出

Python 在Django Autocomplete灯光中使用自定义输出,python,django,django-autocomplete-light,Python,Django,Django Autocomplete Light,我正在尝试为django autocomplete light的输出使用自定义字符串。DAL当前返回模型“str或unicode。我希望返回搜索中的特定字段,而不是str或unicode方法 例如,如果我的模型是这样定义的: class Fruits(models.Model): name = models.CharField() quantity = models.IntegerField() def __str__(self): return sel

我正在尝试为django autocomplete light的输出使用自定义字符串。DAL当前返回模型“strunicode。我希望返回搜索中的特定字段,而不是str或unicode方法

例如,如果我的模型是这样定义的:

class Fruits(models.Model):
    name = models.CharField()
    quantity = models.IntegerField()

    def __str__(self):
        return self.quantity
我想返回一个显示数量和名称的字符串,例如:“香蕉-13”作为一个选项,“苹果-10”作为另一个选项,等等


我可以将字符串作为字段添加到模型中,但我不能将这个新的“连接”字段作为str或unicode。

为什么您不想将
self.quantity
更改为
self.name
?你好,Raja,感谢您的快速响应!实际上,我在最初的帖子中犯了一个错误,没有100%地说明问题所在。我已经用更多的信息编辑了原始帖子。我想实际返回一个字符串,其中包含来自这两个字段的一些信息。我无法将模型的str或unicode更改为我要返回的这种新类型的字符串。