<bdo id='JNUOl'></bdo><ul id='JNUOl'></ul>

    1. <i id='JNUOl'><tr id='JNUOl'><dt id='JNUOl'><q id='JNUOl'><span id='JNUOl'><b id='JNUOl'><form id='JNUOl'><ins id='JNUOl'></ins><ul id='JNUOl'></ul><sub id='JNUOl'></sub></form><legend id='JNUOl'></legend><bdo id='JNUOl'><pre id='JNUOl'><center id='JNUOl'></center></pre></bdo></b><th id='JNUOl'></th></span></q></dt></tr></i><div id='JNUOl'><tfoot id='JNUOl'></tfoot><dl id='JNUOl'><fieldset id='JNUOl'></fieldset></dl></div>
      <legend id='JNUOl'><style id='JNUOl'><dir id='JNUOl'><q id='JNUOl'></q></dir></style></legend>

    2. <small id='JNUOl'></small><noframes id='JNUOl'>

    3. <tfoot id='JNUOl'></tfoot>

        Django get()返回了多个

        Django get() returned more than one(Django get()返回了多个)

          <bdo id='e9oAs'></bdo><ul id='e9oAs'></ul>

            <tfoot id='e9oAs'></tfoot>

                  <legend id='e9oAs'><style id='e9oAs'><dir id='e9oAs'><q id='e9oAs'></q></dir></style></legend>
                1. <i id='e9oAs'><tr id='e9oAs'><dt id='e9oAs'><q id='e9oAs'><span id='e9oAs'><b id='e9oAs'><form id='e9oAs'><ins id='e9oAs'></ins><ul id='e9oAs'></ul><sub id='e9oAs'></sub></form><legend id='e9oAs'></legend><bdo id='e9oAs'><pre id='e9oAs'><center id='e9oAs'></center></pre></bdo></b><th id='e9oAs'></th></span></q></dt></tr></i><div id='e9oAs'><tfoot id='e9oAs'></tfoot><dl id='e9oAs'><fieldset id='e9oAs'></fieldset></dl></div>
                    <tbody id='e9oAs'></tbody>

                  <small id='e9oAs'></small><noframes id='e9oAs'>

                  本文介绍了Django get()返回了多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有此错误,如何修复此错误?

                  get()返回多个事件--它返回%2!

                  你们能帮助我理解这是什么意思吗?或许可以提前告诉我以后如何避免此错误?

                  模型

                  class Event (models.Model):
                      name = models.CharField(max_length=100)
                      date = models.DateField(default='')
                      dicript = models.CharField(max_length=50, default='Описание отсутствует')
                      category = models.ForeignKey(Category,on_delete=models.CASCADE)
                      adress = models.TextField(max_length=300)
                      user = models.ForeignKey(User,related_name="creator",null=True)
                      subs = models.ManyToManyField(User, related_name='subs',blank=True)
                  
                      @classmethod
                      def make_sub(cls, this_user, sub_event):
                          event, created = cls.objects.get_or_create(
                              user=this_user
                          )
                          sub_event.subs.add(this_user)
                  

                  视图

                  def cards_detail (request,pk=None):
                      # if pk:
                      event_detail = Event.objects.get(pk=pk)
                      subs = event_detail.subs.count()
                  
                      # else:
                      #     return CardsView()
                      args = {'event_detail':event_detail,'subs':subs}
                      return render(request,'events/cards_detail.html',args)
                  
                  
                  class CardsView (TemplateView):`
                      template_name = 'events/cards.html'
                  
                      def get (self,request):
                          events = Event.objects.all()
                          return render(request,self.template_name,{'events':events })
                  
                  def subs_to_event (request,pk=None):
                  
                      event = Event.objects.filter(pk=pk)
                      Event.make_sub(request.user,event)
                  
                  
                      return redirect('events:cards')
                  

                  推荐答案

                  from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
                  try:
                      instance = Instance.objects.get(name=name)
                  except (ObjectDoesNotExist, MultipleObjectsReturned):
                      pass
                  

                  get()引发MultipleObjectsReturned如果找到多个对象,则更多信息here。 错误由event_detail = Event.objects.get(pk=pk)引起,请检查您的事件主键是否唯一。

                  这篇关于Django get()返回了多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                2. <tfoot id='fqD22'></tfoot>

                  • <bdo id='fqD22'></bdo><ul id='fqD22'></ul>
                          <tbody id='fqD22'></tbody>

                          1. <legend id='fqD22'><style id='fqD22'><dir id='fqD22'><q id='fqD22'></q></dir></style></legend>
                            <i id='fqD22'><tr id='fqD22'><dt id='fqD22'><q id='fqD22'><span id='fqD22'><b id='fqD22'><form id='fqD22'><ins id='fqD22'></ins><ul id='fqD22'></ul><sub id='fqD22'></sub></form><legend id='fqD22'></legend><bdo id='fqD22'><pre id='fqD22'><center id='fqD22'></center></pre></bdo></b><th id='fqD22'></th></span></q></dt></tr></i><div id='fqD22'><tfoot id='fqD22'></tfoot><dl id='fqD22'><fieldset id='fqD22'></fieldset></dl></div>

                            <small id='fqD22'></small><noframes id='fqD22'>