如何基于外键交叉引用? Django的

时间:2017-05-17 00:27:48

标签: python django django-models filter django-views

我有一个Django应用程序,旅行者'请求'当地人带他出去游览。在他们出去之后,他们为彼此留下了参考,以提及他们是否有乐趣和一些书面描述。

在主页上,我想自动显示旅行者和当地人已经遇到的最后两个请求,并为彼此留下参考。

目前数据来自人口脚本。

这是请求模型:

 class Request(models.Model):
    """traveler requests local to take her out upon liking her profile"""
    traveler = models.ForeignKey(User, related_name='traveler_requests')
    local = models.ForeignKey(User, related_name='local_requested')
    message = models.CharField(max_length=500)
    date = models.DateField()#date of excursion
    local_approval = models.BooleanField(blank=True)

这是参考模型:

class Reference(models.Model):
    author = models.ForeignKey(User, related_name='referencer')#can be local or traveler
    referenced = models.ForeignKey(User, related_name='referencee')#can be local or traveler
    description = models.CharField(max_length=500, default="default reference")
    fun = models.BooleanField() #did you have fun with the person or not?
    date = models.DateField(default=datetime.datetime.now())#in order to pull the last ref on index page
    local = models.BooleanField(default=True) #Is the author a local ?

这是我尝试实现我想要的东西:

reqs = Request.objects.filter(local_approval=True).order_by('-date')[:2]

for i in range(len(reqs)):
    local_references_traveler = Reference.objects.filter(fun=True, author=reqs[i].local, referenced=reqs[i].traveler, local=True).latest()
    traveler_references_local = Reference.objects.filter(fun=True, author=reqs[i].traveler, referenced=reqs[i].local, local=False).latest()

然而,这一般会带回最后两个请求(按日期),我想返回最后两个请求,其中两个人都为彼此留下了引用并提取这些引用。

这似乎有用的唯一方法是在人口脚本中填充大量引用,使应用看起来很糟糕。

谢谢!

1 个答案:

答案 0 :(得分:0)

你必须重构你的模型,最好的方法就是“引用”与“请求”的一对一关系,如下所示:

json_decode

然后你可以试试这个:

class Reference(models.Model):
    request = models.OneToOneField(
        Request,
        on_delete=models.CASCADE,
        primary_key=True,
    )
    author_desc = models.CharField(max_length=500)
    referenced_desc = models.CharField(max_length=500)
    author_fun = models.BooleanField()
    referenced_fun = models.BooleanField()
    author_date = models.DateField(default=datetime.datetime.now())
    referenced_date = models.DateField(default=datetime.datetime.now())

或者您可以制作一个简单的FK来申请,如下:

r = Reference.objects.select_related('request').filter(author_fun=True,referenced_fun=True).order_by('-request__date')
# Don´t forget index or another filter for optimization

然后你可以制作一个注释条款:

class Reference(model.Models):
    request= ForeignKey(Request)
    author = models.ForeignKey(User, related_name='referencer')#can be local or traveler
    # referenced = models.ForeignKey(User, related_name='referencee')#this isn't necesary
    description = models.CharField(max_length=500, default="default reference")
    fun = models.BooleanField() #did you have fun with the person or not?
    date = models.DateField(default=datetime.datetime.now())#in order to pull the last ref on index page
    local = models.BooleanField(default=True) #Is the author a local ?        

...有了这个,你可以获得两个引用的所有“request.id”。