zoukankan      html  css  js  c++  java
  • djnago 官方关系反向查询案例

    模型:
    from django.db import models
    
    class Blog(models.Model):
        name = models.CharField(max_length=100)
        tagline = models.TextField()
    
        def __str__(self):
            return self.name
    
    class Author(models.Model):
        name = models.CharField(max_length=200)
        email = models.EmailField()
    
        def __str__(self):
            return self.name
    
    class Entry(models.Model):
        blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
        headline = models.CharField(max_length=255)
        body_text = models.TextField()
        pub_date = models.DateField()
        mod_date = models.DateField()
        authors = models.ManyToManyField(Author)
        number_of_comments = models.IntegerField()
        number_of_pingbacks = models.IntegerField()
        rating = models.IntegerField()
    
        def __str__(self):
            return self.headline
    

      

    2. related_qurey_name用法:

    ForeignKey.related_query_name

    The name to use for the reverse filter name from the target model. It defaults to the value of related_name or default_related_name if set, otherwise it defaults to the name of the model:

    # Declare the ForeignKey with related_query_name
    class Tag(models.Model):
        article = models.ForeignKey(
            Article,
            on_delete=models.CASCADE,
            related_name="tags",
            related_query_name="tag",
        )
        name = models.CharField(max_length=255)
    
    # That's now the name of the reverse filter
    Article.objects.filter(tag__name="important")
    

    Like related_namerelated_query_name supports app label and class interpolation via some special syntax.

    反向查询:related_name 和不使用任何related_name 和related_query_name

    Following relationships “backward”

    If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above.

    Example:

    >>> b = Blog.objects.get(id=1)
    >>> b.entry_set.all() # Returns all Entry objects related to Blog.
    
    # b.entry_set is a Manager that returns QuerySets.
    >>> b.entry_set.filter(headline__contains='Lennon')
    >>> b.entry_set.count()
    

    You can override the FOO_set name by setting the related_name parameter in the ForeignKey definition. For example, if the Entry model was altered to blog ForeignKey(Blog, on_delete=models.CASCADE, related_name='entries'), the above example code would look like this:

    >>> b = Blog.objects.get(id=1)
    >>> b.entries.all() # Returns all Entry objects related to Blog.
    
    # b.entries is a Manager that returns QuerySets.
    >>> b.entries.filter(headline__contains='Lennon')
    >>> b.entries.count()
  • 相关阅读:
    游戏《翻转方块》小攻略
    净捡软柿子捏--jQuery 遍历方法
    关于兼容
    sublime
    jQuery中json对象与json字符串互换
    css之IE透明度
    关于优化
    html5+css+div随时笔记
    css3学习--border
    JavaScript学习1
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/13884735.html
Copyright © 2011-2022 走看看