Python Django - 模型:查询/过滤器中的 for 循环

Posted

技术标签:

【中文标题】Python Django - 模型:查询/过滤器中的 for 循环【英文标题】:Python Django - Models: for loop in query/filter 【发布时间】:2017-04-21 09:42:08 【问题描述】:

所以我想做一个查询,其中字段等于 for 循环中对象的属性。这是一个使用 sqlAlchemy 的工作示例/示例:

you =   session.query(Users).filter_by(id=login_session['userid']).first()

friends = session.query(Friends).filter_by(user_id=you.id).all()

dashboard = session.query(Markers).filter(Markers.owner.in_([f.friend_id for f in friends)]).all()

我将如何在 django 中做到这一点?

这是我尝试过的:

you = Accounts.objects.get(uname = request.session['username'])

following = Follows.objects.filter(userid=you.id)

POSTS = Posts.objects.filter(ownerid = [f.follow_id for f in following])

这就是我得到的:

Traceback:

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "C:\Users\waite-ryan-m\Desktop\epsity\webapp\views.py" in profileMain
  102.             POSTS = Posts.objects.filter(ownerid = [f.follow_id for f in following])

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\query.py" in filter
  790.         return self._filter_or_exclude(False, *args, **kwargs)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  808.             clone.query.add_q(Q(*args, **kwargs))

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\sql\query.py" in add_q
  1243.         clause, _ = self._add_q(q_object, self.used_aliases)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1269.                     allow_joins=allow_joins, split_subq=split_subq,

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1203.             condition = self.build_lookup(lookups, col, value)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\sql\query.py" in build_lookup
  1099.                 return final_lookup(lhs, rhs)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\lookups.py" in __init__
  19.         self.rhs = self.get_prep_lookup()

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\lookups.py" in get_prep_lookup
  57.         return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
  1860.         return super(IntegerField, self).get_prep_lookup(lookup_type, value)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
  744.             return self.get_prep_value(value)

File "C:\Users\waite-ryan-m\Desktop\WPython\WinPython-64bit-2.7.12.1Zero\python-2.7.12.amd64\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  1854.         return int(value)

Exception Type: TypeError at /main/
Exception Value: int() argument must be a string or a number, not 'list'

我的模型:

class Accounts(models.Model):

    uname = models.CharField(max_length = 20, default = '')
    displayname = models.CharField(max_length = 30, default = '')
    email = models.CharField(max_length = 50, default = '')
    pswrd = models.CharField(max_length = 50, default = '')
    provider = models.CharField(max_length = 20, default = '')
    provider_id = models.CharField(max_length = 100, default = '')
    avi = models.CharField(max_length = 500, default = '/static/img/anon2.png')
    background = models.CharField(max_length = 500, default = '/static/img/bg-default.jpg')
    gender = models.CharField(max_length = 25, default = '')
    phone = models.CharField(max_length = 25, default = '')

    type = models.CharField(max_length = 20, default = '')
    status = models.CharField(max_length = 9, default = 'public')
    # status: either public, private or deleted

    official = models.IntegerField(default = 0)
    # official: 1 = True | 0 = False

    interests = models.CharField(max_length = 1725, default = '')
    seeking = models.CharField(max_length = 1725, default = '')

    bio_desc = models.CharField(max_length = 150, default = '')
    bio_link = models.CharField(max_length = 150, default = '')
    #bio_link_name = models.CharField(max_length = 100, default = '')

    date_created = models.DateField( default = datetime.datetime.now )
    last_active = models.DateField(auto_now=True)

    @property
    def serialize(self):
         # Returns Data Object In Proper Format
        return 
            'userid': self.id,
            'uname': self.uname,
            'displayname': self.displayname,
            'avi': self.avi,
            'status': self.status,
            'background': self.background,
        

    @property
    def get_bio(self):
         # Returns Data Object In Proper Format
        return 
            'desc': self.bio_desc,
            'link': self.bio_link,
            #'linkName': self.bio_link_name,
        

    @property
    def get_info(self):
         # Returns Data Object In Proper Format
        return 
            'interests': self.interests,
            'seeking': self.seeking,
            #'linkName': self.bio_link_name,
        

    class Meta:
        db_table = "accounts"

# --- #

class Follows(models.Model):

    userid = models.IntegerField(blank = False, default = 0)
    user_rel = models.ForeignKey(Accounts, default = 0, related_name = "user_rel")

    follow_id = models.IntegerField(blank = False, default = 0)
    follow_rel = models.ForeignKey(Accounts, default = 0, related_name = "follow_rel")

    date_created = models.DateField( default = datetime.datetime.now )

    @property
    def serialize(self):
         # Returns Data Object In Proper Format
        return 
            'fid': self.id,

            'userid': self.userid,
            'user': self.user_rel.serialize,
            'followid': self.follow_id,
            'follow': self.follow_rel.serialize,
            'date_created': self.date_created
            #'linkName': self.bio_link_name,
        

    class Meta:
        db_table = "follows"

# ---

class Posts(models.Model):

    OwnerType = (
        ('Account', 'Account'),
        ('Group', 'Group'),
    )

    PostTypes = (
        ('Text', 'Text'),
        ('Photo', 'Photo'),
        ('Audio', 'Audio'),
        ('Video', 'Video'),
    )

    ownerid = models.IntegerField(blank = False, default = 0)
    owner_type = models.CharField(choices = OwnerType, blank = False, default = '', max_length = 50)

    title = models.CharField(max_length = 500, default = '')
    contents = models.CharField(max_length = 500, default = '')
    link = models.CharField(max_length = 500, default = '')

    attachment = models.CharField(max_length = 500, default = '')
    attachment_type = models.CharField(max_length = 500, default = '')

    post_type = models.CharField(max_length = 20, choices = PostTypes, default = '')
    status = models.CharField(max_length = 20, default = 'public') # either public, private, or deleted

    date_created = models.DateField( default = datetime.datetime.now )
    last_active = models.DateField(auto_now=True)

    @property
    def serialize(self):
        return 
            'p_id': self.id,

            'ownerid': self.ownerid,
            'owner': returnModelSerialized( self.owner_type , self.ownerid ),
            'owner_type': self.owner_type,
            'title': self.title,
            'contents': self.contents,
            'attachment': self.attachment,
            'attachment_type': self.attachment_type,
            'link': self.link,
            'post_type': self.post_type,
            'status': self.status,
            'date_created': self.date_created,
            'last_active': self.last_active
        

    class Meta:
        db_table = "posts"

# ---

【问题讨论】:

你 = Accounts.objects.get(uname = request.session['username']) following = Follows.objects.filter(userid=you.id) POSTS = Posts.objects.filter(ownerid = [f.follow_id for f in following]) cmets 中的代码很难阅读。更新问题以包含代码。还请告诉我们代码做了什么,以及它与您想要的有何不同。 当然!添加了模型 【参考方案1】:

您收到错误是因为您的过滤器需要单个实例的 id,而不是 id 列表。

您可以使用__in 过滤列表中的 id:

POSTS = Posts.objects.filter(ownerid__in=[f.follow_id for f in following])

【讨论】:

【参考方案2】:

Alasdair's answer 是正确的。比较属性时不应传递列表。我想添加一个改进。

当您使用外键时,您不需要单独的 user_id 类型字段。您也可以通过外键访问它。 Django Foreign Key 在您正在使用的应用程序中非常有用。 Here 是包含与您的示例类似的文档。通过它。希望这可能会有所帮助。

【讨论】:

以上是关于Python Django - 模型:查询/过滤器中的 for 循环的主要内容,如果未能解决你的问题,请参考以下文章

Django中模型

Django模型查询集过滤器作为一个函数

基于查询字符串的Django模型过滤器[关闭]

Django,来自模型方法的查询过滤

django基础知识之模型查询:

python-django-查询详解