2016-03-27 37 views
0

我試圖創建一個Django查詢,其中結果是來自Item表中的所有條目,其中沒有對於給定的Seen表中的表項的條目用戶。Django的查詢,排除如果一個表中的ID是另一個

我的Django模型是這樣的:

from django.db import models 
 
from django.contrib.auth.models import User 
 

 
# The keywords that can be associated with an item (e.g. Animals, Scenery, Buildings, Art, ...) 
 
class Keyword(models.Model): 
 
    name = models.CharField(max_length=30) 
 

 
    # This needed to show the name and not the text 'keyword object' on the Admin page 
 
    def __str__(self): 
 
     return format(self.name) 
 

 
# Items. E.g. Vines, YouTube, Vimeo, Photo... 
 
class Item(models.Model): 
 

 
    ITEM_TYPES = (
 
     ('V', 'Vine'), 
 
     ('Y', 'YouTube'), 
 
     ('P', 'Photo'),   # Photo is stored by us on a CDN somewhere 
 
     ('F', 'Flickr'), 
 
     ('I', 'Instagram'), 
 
     ('D', 'DeviantArt'), 
 
     ('5', '500px'), 
 
    ) 
 
    owner   = models.ForeignKey(User, on_delete=models.CASCADE)  # Id of user who owns the item 
 
    url    = models.CharField(max_length=250, default='')   # URL of where item resides (e.g. Vine or YouTube url) 
 
    item_type  = models.CharField(max_length=1, choices=ITEM_TYPES) # Type of item (e.g. Vine|YoutTube|Instagram|etc.) 
 
    keywords  = models.ManyToManyField(Keyword, related_name='keywords') 
 
                      # E.g. Art, Travel, Food, etc. 
 
    credits_applied = models.IntegerField(default=10, help_text='Total number of credits applied to this item including any given by VeeU admin') 
 
                      # Records the total number of credits applied to the Item 
 
    credits_left = models.IntegerField(default=10, help_text='The number of credits still remaining to show the item') 
 
                      # Number of credits left (goes down each time item is viewed 
 
    credits_gifted = models.IntegerField(default=0, help_text='The number of credits this item has been gifted by other users') 
 
                      # Number of credits users have gifted to this item 
 
    date_added  = models.DateTimeField(auto_now_add=True)    # When item was added 
 
    active   = models.BooleanField(default=True, help_text='If you mark this item inactive please say why in the comment field. E.g. "Inapproriate content"') 
 
                      # True if item is available for showing 
 
    comment   = models.CharField(max_length=100, blank=True)   # Comment to be applied if item is inactive to say why 
 

 
    # Add defs here for model related functions 
 

 
    # This to allow url to be a clickable link 
 
    def item_url(self): 
 
     return u'<a href="%s">%s</a>' % (self.url, self.url) 
 
    item_url.allow_tags = True 
 

 
    def __str__(self): 
 
     return '%s: %s' % (self.owner, self.url) 
 

 
# Record of which items have been viewed, when, and whether they were liked or not 
 
class Seen(models.Model): 
 
    item_id   = models.ForeignKey(Item, on_delete=models.CASCADE)  # id of the item that has been seen 
 
    user_id   = models.ForeignKey(User, on_delete=models.CASCADE)  # id of user who viewed it 
 
    date_seen  = models.DateTimeField(auto_now_add=True)    # When item was viewed 
 
    liked   = models.BooleanField(help_text='If the item was liked this is set to true') 
 
                      # Flag True if item was liked 
 

 
    # Add defs here for model related functions

我想我需要使用看起來像這樣的查詢:

Item.objects.exclude(
 
    seen=Seen.objects.filter(
 
     user_id=23, 
 
     item_id=<id of item from Item table>, 
 
    ), 
 
)

我無法解決的是如何將項目表中的每個項目與相同項目標識的Seen表格中的項目進行比較。

回答

1

通過訪問視圖中的request對象,您可以從Seen表中獲取當前用戶的item_id列表,並使用Django的in運算符篩選。

unseen_items = Item.objects.exclude(
    pk__in=Seen.objects.filter(user_id=request.user).values_list(
     'item_id', flat=True 
    ) 
) 

inhere信息對values_list信息見here

+1

非常感謝! –

相關問題