代码之家  ›  专栏  ›  技术社区  ›  Samir Tendulkar

如何使forloops只在django python中通过唯一的值运行

  •  0
  • Samir Tendulkar  · 技术社区  · 7 年前

    简介: 我有一个叫 event 有一把钥匙 post . 它的工作方式是: 例子 用户创建了一个名为“日食”的帖子。任何数量的用户都可以基于该日志创建事件。所以如果有人上了那篇文章的详细页面。有个按钮叫 See all Events 他们可以看到所有主持日食活动的人。假设有5个帖子(关于滑雪、徒步旅行等不同主题),其中3个帖子有10个活动。2个岗位没有活动。在我的主页上,我有一个写事件的部分。在主页的事件部分,我想展示3个有事件而不是30个事件的帖子,我该怎么做呢?这样我就不会多次访问数据库了

    下面是 VIEW

    class EventView(ListView):
        model = Event
        template_name = 'home.html'    
        context_object_name = 'all_events' 
    
        def get_queryset(self): 
            for post in Post.objects.all():
                 return post.event_post.all() #event_post is a related_name of post see models below
    
    #This is incorrect it shows the post 30 times. 10 times for each event. I want to see only 3 posts. They can see the events when they click to open the post
    

    我如何才能做到这一点:

    我的 事件模型

    class Event(models.Model):
            user = models.ForeignKey(User, related_name='host')
            post = models.ForeignKey(Post, related_name='event_post')
            price = models.DecimalField(max_digits=5, decimal_places=2)
            stock = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(15)])
            initial_stock = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(15)], default=1)
            date = models.DateField()
            time_from = models.TimeField()
            time_to = models.TimeField()
            available = models.BooleanField(default=True)
            note = models.CharField(max_length=350, blank=True, null=True)
            created = models.DateTimeField(auto_now_add=True)
            updated = models.DateTimeField(auto_now=True)
    

    我的 POST模型

    class Post(models.Model):
        user = models.ForeignKey(User, related_name='posts')
        group = models.ForeignKey(Group, related_name='posts')
        created_at = models.DateTimeField(auto_now=True)
        title = models.CharField(max_length=250, unique=True)
        slug = models.SlugField(allow_unicode=True, unique=True)
        message = models.TextField()
        message_html = models.TextField(editable=False)
        post_image = models.ImageField(upload_to='post_images/')
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Oh Great One    7 年前

    Post.objects.all() selected_post.event_post_set.all()

    PostView Post EventView

    post = Post.objects.get(pk=passed_in_post_pk)
    ...
    return post.event_post_set.all()
    

    .exists() if

    post_events_exist = post.event_post_set.all().exists()
    if post_events_exist:
        # there is at least 1 event in the query set.
    

    # Rename EventView --> PostView
    # Inside of your now PostView
    # All we need to do is return all your instances of Post
    # and go to your post_template.html (shows the posts, you call it events)
    return Post.objects.all()
    
    # Inside of a new view called EventView
    # We can get to this view by clicking one of the posts above.
    # This assumes we send a parameter through url in template.
    # This will send user to event_template.html (shows all events for that post)
    post = Post.objects.get(pk=passed_in_post_pk)
    return post.post_event_set.all()
    

    <!-- assuming all_posts = Post.objects.all() -->
    {% for post in all_posts %}
        {{ post.title }}
        <a href="{{ url_that_links_to_event_view post.pk }}">Info</a>
        <!-- this will send post.pk as a parameter to that view (so long as your accept a parameter in your urls.py for this link -->
    {% endfor %}
    

    {% for event in all_events %}
        {{ event.price }}
    {% endfor %}
    

        2
  •  0
  •   Samir Tendulkar    7 年前

    for post in Post.objects.all():
        if post.event_post.exists():
            print.post.title: 
    

    推荐文章