Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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将字符串转换为类_Python_Mysql_Django_Python 2.7 - Fatal编程技术网

Python将字符串转换为类

Python将字符串转换为类,python,mysql,django,python-2.7,Python,Mysql,Django,Python 2.7,我想对django用户表执行如下查询: u = User.objects.filter(member__in = member_list) 其中: class Member(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) dob = models.DateField('Date of Birth', blank=True, null=True) 而member\u list是

我想对django用户表执行如下查询:

u = User.objects.filter(member__in = member_list)
其中:

class Member(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    dob = models.DateField('Date of Birth', blank=True, null=True)
member\u list
是一个合格成员的列表

查询工作正常,但问题是我实际上不知道
member
模型被称为
member
。它可以被称为任何东西

我将所需模型的名称存储在名为
Category
的模型中。我通过
content\u type
链接到模型名称
Category
定义为:

class Category(models.Model):
    name = models.CharField('Category', max_length=30)
    content_type = models.ForeignKey(ContentType)
    filter_condition = JSONField(default="{}", help_text=_(u"Django ORM compatible lookup kwargs which are used to get the list of objects."))
    user_link = models.CharField(_(u"Link to User table"), max_length=64, help_text=_(u"Name of the model field which links to the User table.  'No-link' means this is the User table."), default="No-link")

    def clean (self):
        if self.user_link == "No-link":
            if self.content_type.app_label == "auth" and self.content_type.model == "user":
                pass
            else:
                raise ValidationError(
                    _("Must specify the field that links to the user table.")
                    )
        else:
            if not hasattr(apps.get_model(self.content_type.app_label, self.content_type.model), self.user_link):
                raise ValidationError(
                    _("Must specify the field that links to the user table.")
                    )            

    def __unicode__(self):
        return self.name

    def _get_user_filter (self):
        return str(self.content_type.app_label)+'.'+str(self.content_type.model)+'.'+str(self.user_link)+'__in'

    def _get_filter(self):
        # simplejson likes to put unicode objects as dictionary keys
        # but keyword arguments must be str type
        fc = {}
        for k,v in self.filter_condition.iteritems():
            fc.update({str(k): v})
        return fc

    def object_list(self):
        return self.content_type.model_class()._default_manager.filter(**self._get_filter())

    def object_count(self):
        return self.object_list().count()

    class Meta:
        verbose_name = _("Category")
        verbose_name_plural = _("Categories")
        ordering = ('name',)
因此,我可以检索链接到用户的模型的名称,但我需要将其转换为一个类,我可以在查询中包含该类

我可以创建一个对象x=category.content\u type.model\u class(),它给了我
,但是当我执行查询
s=User.objects.filter(x=c.category.object\u list())
时,我发现错误无法将关键字“x”解析到字段中。


欢迎任何想法。

过滤器参数的左侧是关键字,而不是python对象,因此x被视为“x”,Django需要一个名为x的字段

为了解决这个问题,您可以确保
x
是一个字符串,然后使用python
**kwarg
语法:

s = User.objects.filter(**{x: c.category.object_list()})

多亏了。

过滤器参数的左侧是一个关键字,而不是python对象,因此x被视为“x”,Django需要一个名为x的字段

为了解决这个问题,您可以确保
x
是一个字符串,然后使用python
**kwarg
语法:

s = User.objects.filter(**{x: c.category.object_list()})
谢谢你的帮助