简体   繁体   English

如何通过 Django 模型进行查询并在没有 ManyToMany 关系的情况下访问另一个 Django 模型?

[英]How to query through a Django model and get access to another Django model without a ManyToMany relationship?

I have a model called WatchList with an object called listing which corresponds to the Listings Model through a Foreign Key.我有一个名为 WatchList 的模型,其中包含一个名为 Listing 的对象,该对象通过外键对应于列表模型。 I want to be able to query through the WatchList Model to get all the listings on a user's particular watchlist and display all the objects of all the listings on a webpage.我希望能够通过 WatchList 模型进行查询,以获取用户特定监视列表中的所有列表,并在网页上显示所有列表的所有对象。 I do not want to do this using a ManyToMany Field because I just got a different part of my web application that deals with the WatchList Model to work.我不想使用 ManyToMany 字段来执行此操作,因为我刚刚获得了 Web 应用程序的一个不同部分,它处理 WatchList 模型的工作。 Is there any other way to do this?有没有其他方法可以做到这一点?

views.py视图.py

def watchlist(request):
    watchlists = WatchList.objects.filter(user=request.user)
    for listing in watchlists.listing:
        listings_needed = watchlists.listing()
    watchlist_listing = watchlists.get(listing)
    listings = Listings.objects.all().filter(watchlist_listing)
    return render(request, "auctions/watchlist.html",{
        "listings": listings
    })

models.py模型.py

class Listings(models.Model):
    CATEGORY = [
    ("Miscellaneous", "Miscellaneous"),
    ("Movies and Television", "Movies and Television"),
    ("Sports", "Sports"),
    ("Arts and Crafts", "Arts and Crafts"),
    ("Clothing", "Clothing"),
    ("Books", "Books"),
]
    title = models.CharField(max_length=64)
    description = models.CharField(max_length=500)
    bid = models.DecimalField(max_digits=1000000000000, decimal_places=2)
    image = models.URLField(null=True, blank=True)
    category = models.CharField(max_length=64, choices=CATEGORY, default=None)
    user = models.ForeignKey(User, on_delete=models.CASCADE, default="")

class WatchList(models.Model):
    listing = models.ForeignKey(Listings, on_delete=models.CASCADE, default="")
    user = models.ForeignKey(User, on_delete=models.CASCADE, default="")

This error is also occurring with the current code:当前代码也出现此错误:

arg, value = filter_expr
TypeError: cannot unpack non-iterable function object

This error is caused by watchlist_listing = watchlists.get(listing) .此错误是由watchlist_listing = watchlists.get(listing)引起的。

I also tried using a for loop, but that didn't work.我也尝试使用 for 循环,但没有奏效。

You can get all Listings for a User where there is an entry in the WatchList model by following the relationship backwards in a Listings queryset - Listings.objects.filter(watchlist__user=user)您可以通过在列表查询集中向后跟踪关系来获取在 WatchList 模型中有条目的用户的所有列表 - Listings.objects.filter(watchlist__user=user)

def watchlist(request):
    listings = Listings.objects.filter(watchlist__user=request.user)
    return render(request, "auctions/watchlist.html",{
        "listings": listings
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM