<bdo id='Ru0ws'></bdo><ul id='Ru0ws'></ul>
  • <legend id='Ru0ws'><style id='Ru0ws'><dir id='Ru0ws'><q id='Ru0ws'></q></dir></style></legend>
  • <small id='Ru0ws'></small><noframes id='Ru0ws'>

        <tfoot id='Ru0ws'></tfoot>

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

        包括其他字段作为外键的选择,Django

        Include other field as choices to foreign key, Django(包括其他字段作为外键的选择,Django)
        • <tfoot id='Lsf56'></tfoot>

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

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

              <tbody id='Lsf56'></tbody>

              • <bdo id='Lsf56'></bdo><ul id='Lsf56'></ul>
                  本文介绍了包括其他字段作为外键的选择,Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个模型如下:

                  class FlightSchedule(models.Model):
                      tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
                      flight_number = models.CharField(max_length=30, null=False, blank=False)
                      flight_group_code = models.ForeignKey(FlightGroup, null=False, blank=False)
                      origin_port_code = models.ForeignKey(Port, null=False, related_name="Origin", blank=False)
                      destination_port_code = models.ForeignKey(Port, null=False, related_name="Destination", blank=False)
                      flight_departure_time = models.TimeField()
                      start_date = models.DateField()
                      end_date = models.DateField()
                  
                      def __unicode__(self):
                          return u'%s' % self.flight_number
                  
                      class Meta:
                          verbose_name_plural = "flights Schedule"
                  
                  
                  class PosFlightSchedule(models.Model):
                      tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
                      pos_flight_number = models.ForeignKey(FlightSchedule, max_length=30, null=False, blank=False,
                                                        related_name='pos_flight_number')
                      pos_flight_departure_time = models.ForeignKey(FlightSchedule, max_length=30,
                                                                related_name='pos_flight_departure_time')
                      pos_route_id = models.ForeignKey(FlightScheduleDetail, null=False, blank=False, related_name='pos_route_id')
                      pos_flight_date = models.ForeignKey(FlightScheduleDetail, null=False, blank=False, related_name='pos_flight_date')
                      pax_count = models.IntegerField(null=True)
                  
                      def __unicode__(self):
                          return u'%s' % self.pos_flight_number
                  
                      class Meta:
                          verbose_name_plural = "Flights Schedule"
                  

                  对于 pos_flight_departure_time ,我需要来自 FlightSchedule 类的 flight_departure_time 的选择.但我在下拉列表中得到 flight_number 值.为了获得 flight_departure_time 值,我需要改变什么?这些类来自单个 django 项目中的不同应用程序.所以他们有两个管理文件.

                  For the pos_flight_departure_time , I need the choices from flight_departure_time from the FlightSchedule class. But I get the flight_number values in the drop down. What do I have to change, to get the flight_departure_time values? The classes are from different apps in a single django project. So they have two admin files.

                  推荐答案

                  不,你实际上并不需要那个.您在第二个模型中只需要一个外键来 FlightScheduleDetail 并且您只需要一个外键来 FlightSchedule

                  No you don't actually need that. You need only one foreign key in your second model to FlightScheduleDetail and you need just one foreign key to FlightSchedule

                  class PosFlightSchedule(models.Model):
                      tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
                      flight = models.ForeignKey(FlightSchedule, null=False, blank=False,related_name='pos_flight_number')
                                                                related_name='pos_flight_departure_time')
                      pos_route_id = models.ForeignKey(FlightScheduleDetail, null=False, blank=False, related_name='pos_route_id')
                              pax_count = models.IntegerField(null=True)
                  
                      def __unicode__(self):
                          return u'%s' % self.pos_flight_number
                  
                      class Meta:
                          verbose_name_plural = "Flights Schedule"
                  

                  然后在第一个模型中声明的所有字段自动变为可用于PosFlightSchedule

                  Then all the fields declared in the first model automatically become available to PosFlightSchedule

                  例如你可以这样做

                  p = PosFlightSchedule.objects.all()[0]
                  print (p.flight.flight_number)
                  print (p.flight.pos_flight_departure_time)
                  

                  等等.

                  这是正确的做法.

                  这篇关于包括其他字段作为外键的选择,Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                      <tbody id='BLxRa'></tbody>
                      <tfoot id='BLxRa'></tfoot>
                      • <bdo id='BLxRa'></bdo><ul id='BLxRa'></ul>

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

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

                            <legend id='BLxRa'><style id='BLxRa'><dir id='BLxRa'><q id='BLxRa'></q></dir></style></legend>