Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

Error "No Models matches the given query" after using get_next_by_FOO django

When using get_next_by_Foo, it doesnt show content in template file. I get data from this query: request.session['contract_posts_search'] is results I get from filtering in another view.

 def contract_detail_test(request, contract_id=None):
        context = {}
        contract_posts_search=request.session['contract_posts_search']
        contract_filtered=[]
        for i in range(len(contract_posts_search)):
            contract_filtered.append(contract_posts_search[i]["fields"]["contract"])
        contract_posts_search= Contracts.objects.filter(contract__in=contract_filtered).distinct()
        contract_posts = get_object_or_404(contract_posts_search, contract=contract_id)

After having contract_post, I use get_next_by_created_at:

if contract_posts is not None:
        print(contract_posts)
        try:
            the_next = contract_posts.get_next_by_created_at()
        except:
            the_next=None
        try:  
            the_prev = contract_posts.get_previous_by_created_at()
        except:
            the_prev=None
        context = { "contract_posts": contract_posts,
                    "the_next" : the_next,
                    "the_prev": the_prev,
        }
        return render(request, "contract_detail_test.html", context)

My ulr:

path('contract_detail_test/<str:contract_id>/', contract_detail_test, name="contract_detail_test")

My model:

class Contracts(models.Model):
    id=models.AutoField(primary_key=True)
    contract=models.CharField(max_length=255,blank=True, null=True)
    name=models.CharField(max_length=255,blank=True, null=True)
    debt=models.IntegerField(blank=True, null=True)
    created_at=models.DateTimeField(auto_now_add=True,blank=True)
    updated_at=models.DateTimeField(auto_now_add=True,blank=True)
    objects=models.Manager()

    class Meta:
        ordering=["-created_at","-id"]

    def get_absolute_url(self):
        return "/contract_detail_test/%s/" % self.contract

For example: I filter contract contain "2016" and get 10 results, using request_session in another view, I can print out 10 results. Then I use get_next_by_created_at, It will show first result correctly, but after clicking, it will show this error. There is no contract number 20170709-0010161 in contract_posts

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/contract_detail_test/20170709-0010161/
Raised by:  test_upload_filter.views.contract_detail_test
No Contracts matches the given query.
question from:https://stackoverflow.com/questions/66047592/error-no-models-matches-the-given-query-after-using-get-next-by-foo-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...