Py学习  »  Django

如何遍历django模型字段,并根据条件添加值

Ahmed Wagdi • 5 年前 • 1788 次点击  

我有一个模型,它模拟了一条多达30个车站的列车线,因此该模型有30个可空字段。

模型.py

class TempLine(models.Model):
    picking_mode = models.IntegerField(default=1)
    start_station = models.CharField(max_length=2000)
    end_station = models.CharField(max_length=2000, null=True)
    station_1 = models.CharField(max_length=2000, null=True)
    station_2 = models.CharField(max_length=2000, null=True)
    station_3 = models.CharField(max_length=2000, null=True)
    station_4 = models.CharField(max_length=2000, null=True)
    station_5 = models.CharField(max_length=2000, null=True)
    station_6 = models.CharField(max_length=2000, null=True)
    station_7 = models.CharField(max_length=2000, null=True)
    station_8 = models.CharField(max_length=2000, null=True)
    station_9 = models.CharField(max_length=2000, null=True)
    station_10 = models.CharField(max_length=2000, null=True)
    station_11 = models.CharField(max_length=2000, null=True)
    station_12 = models.CharField(max_length=2000, null=True)
    station_13 = models.CharField(max_length=2000, null=True)
    station_14 = models.CharField(max_length=2000, null=True)
    station_15 = models.CharField(max_length=2000, null=True)
    station_16 = models.CharField(max_length=2000, null=True)
    station_17 = models.CharField(max_length=2000, null=True)
    station_18 = models.CharField(max_length=2000, null=True)
    station_19 = models.CharField(max_length=2000, null=True)
    station_21 = models.CharField(max_length=2000, null=True)
    station_22 = models.CharField(max_length=2000, null=True)
    station_23 = models.CharField(max_length=2000, null=True)
    station_24 = models.CharField(max_length=2000, null=True)
    station_25 = models.CharField(max_length=2000, null=True)
    station_26 = models.CharField(max_length=2000, null=True)
    station_27 = models.CharField(max_length=2000, null=True)
    station_28 = models.CharField(max_length=2000, null=True)
    station_29 = models.CharField(max_length=2000, null=True)
    station_30 = models.CharField(max_length=2000, null=True)

使用ajax请求逐个添加数据。

所以我必须从 station_1 …检查是否没有,添加..如果没有..去下一个吧。

以下是我尝试的方法:

def adding_inline_stations(request):
    in_line_station = request.GET.get('inLine_stations', None)
    obj = TempLine.objects.filter()[0]
    for f in obj._meta.get_fields[3:]:
        if f is None:
            f = in_line_station
            f.save()
        else:
            pass

返回错误的 TypeError: 'method' object is not subscriptable

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48339
 
1788 次点击  
文章 [ 2 ]  |  最新文章 5 年前
heemayl
Reply   •   1 楼
heemayl    5 年前

首先是错误: <Model>._meta.get_fields 是方法,而不是属性;因此您需要:

for f in obj._meta.get_fields()[3:]:
# Note the call:             ^^

现在,你的设计似乎不对。你所做的基本上是要求与 Station 模型所以,创建一个 车站 带e.g.a.的模型 name 包含站点名称的字段(以及其他需要的字段)。还要确保你考虑到与 Line 模型清晰;乍一看,对我来说,它看起来像一个多对多。

Sayse
Reply   •   2 楼
Sayse    5 年前

你应该做一个车站模型。当你只需要车站的名字时,它很快就会变成需要一个位置、开放时间等。

一旦您创建了这样一个模型(即使它现在只有一个带有名称的字段),就可以与您的行建立多对多关系,并以与任何其他相关模型字段相同的方式访问它们。

作为一般的编程规则,如果您命名变量 variable_n ,然后是时候重新考虑是否需要将这些对象存储在某种类型的集合中