简体   繁体   English

Django 基于另一个没有指定关系的表过滤查询集

[英]Django filter a queryset based on another table without specified relationship

Ok so this is probably a bit of an odd question, but here is my problem.好的,所以这可能是一个奇怪的问题,但这是我的问题。 With out getting into too much detail I need to send a notice to properties but only if there is a corresponding title search.在没有涉及太多细节的情况下,我需要向属性发送通知,但前提是有相应的标题搜索。 Here are my models.这是我的模型。 There is specified relationship between the two.两者之间有明确的关系。 But they share a common value in a common field.但它们在一个共同的领域具有共同的价值。 They both have a properties parcel number (the parcel number for the notice is in a related field accessible by the foreign key)他们都有一个属性包裹号(通知的包裹号在外键可访问的相关字段中)

class Notice(models.Model):
    status_choice = [
        ("SendNotice", "SendNotice"),
        ("NoticeSent", "NoticeSent")
    ]

    violation_fk = models.OneToOneField(Violation, on_delete=models.CASCADE)
    date_created = models.DateTimeField(auto_now_add=True)
    notice_number = models.CharField(max_length=10)
    date_sent = models.DateTimeField(blank=True, null=True)
    status = models.CharField(choices=status_choice, max_length=10)
    instructions = models.TextField(blank=True, null=True)


class TitleSearch(models.Model):
    address = models.CharField(max_length=50)
    parcel_number = models.CharField( max_length=24)
    title_search_date = models.DateField(auto_now=False, auto_now_add=False)
    expiration_date = models.DateField(auto_now=False, auto_now_add=False)
    owner = models.CharField(max_length=100)
    attn_line = models.CharField(max_length=150, blank=True)
    street_address = models.CharField(max_length=150)
    city = models.CharField(max_length=25)
    state = models.CharField(max_length=2)
    zip_code = models.CharField(max_length=5)

So.所以。 here is my problem.这是我的问题。 I cannot send a notice to a property without a title search.我无法在没有产权搜索的情况下向房产发送通知。 BUT the title searches expire after a set period of time, so I don't necessarily want to tie them together.但是标题搜索会在一段时间后过期,所以我不一定想把它们联系在一起。

I am trying to get a queryset that will select all the notices that have a status of "SendNotice" AND have a non expired title search on file with the same parcel number.我正在尝试获取一个查询集,该查询集将 select 所有状态为“SendNotice”并且在文件中具有相同包裹号的未过期标题搜索的通知。 Sort of a queryset check to make sure the parcel number is IN the list of active title searches.查询集检查的排序,以确保宗地号在活动标题搜索列表中。

Thank you,谢谢,

Ok so in the meantime I have found somewhat of a janky way to work around my problem.好的,与此同时,我发现了一种解决问题的笨拙方法。 It works but its a bit ugly.它有效,但有点难看。

So I couldn't find a way without creating a relationship between the two models to check for the presence of a valid title search from the notice.因此,如果不创建两个模型之间的关系,我就无法找到一种方法来检查通知中是否存在有效的标题搜索。

as a work around I queried for all Title searches that were not expired and converted to a list of parcel numbers (the key I am using to look up across tables)作为一种解决方法,我查询了所有未过期的标题搜索并转换为包裹号列表(我用来跨表查找的键)

# Get list of Title Search for jank in query
active_title_searches = TitleSearch.objects.filter(expiration_date__gte=timezone.now())
active_title_searches = active_title_searches.values_list("parcel_number", flat=True)

Then I added to the filter a __in search to check to see if the parcel number from a parent table is in the list of parcel numbers of active title searches we created earlier.然后我在过滤器中添加了一个 __in 搜索,以检查父表中的宗地号是否在我们之前创建的活动标题搜索的宗地号列表中。

# grab only notices that have title searches
ts_junk_notice = Notice.objects.filter(status="SendNotice", violation_fk__parcel_number__in=active_title_searches)

and that seems to do the trick.这似乎可以解决问题。 its sort of like a sub query to check if there is an active title search but I was reading through the django docs somewhere and it said some dbs had a hard time performing nested querys and you should do 2 simple queries instead.它有点像一个子查询来检查是否有活动的标题搜索,但我正在某处阅读 django 文档,它说一些 dbs 很难执行嵌套查询,你应该做 2 个简单的查询。 Hence the build a list and check within it.因此建立一个列表并在其中检查。

I'm not sure if that helps anyone but hopefully it does.我不确定这是否对任何人有帮助,但希望确实如此。

ps I also found this package from someone else describing a similar problem that could also help you out. ps 我还从其他人那里找到了这个 package,描述了一个类似的问题,也可以帮助你。 https://www.reddit.com/r/django/comments/7fvlf7/join_two_models_without_a_foreign_key/ https://www.reddit.com/r/django/comments/7fvlf7/join_two_models_without_a_foreign_key/

https://github.com/martsberger/django-joinfield https://github.com/martsberger/django-joinfield

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

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