Django:如何根据外键数据在模型中创建一些变量?

Django: How to create some variable in model based on the foreign key data?

我是 Django 的新手,现在我正在做我的第一个项目:一些电影院的座位预订系统。为此,我创建了 3 个模型:Movie、Hall 和 Event。 Hall生成坐图为数组。

class Hall(models.Model):
HALL_NAME = (
    ('A', 'Hall A'),
    ('B', 'Hall B'),
    ('C', 'Hall C')
)
hall_name = models.CharField(choices=HALL_NAME, max_length=30)
hall_structure = models.CharField(max_length=10000, default=generate_sits(hall_name))

@property
def get_sits(self):
    if self.hall_name == 'A':
        return generate_sits('A')
    if self.hall_name == 'Hall B':
        return generate_sits('B')
    if self.hall_name == 'Hall C':
        return generate_sits('C')
    else:
        return "lol"

def __str__(self):
    return f'{self.hall_name}'

之后我想为事件模型创建一个变量,它采用 sits 的数组结构,将其转换为字典(示例 ['A1':'empty','A2': 'empty', ...] )。我希望将来有人应该在 vebsite 上预订座位时覆盖此变量 (['A1': 'reserved']).

这是我的事件模型:

class Event(models.Model):

EVENT_DAYS = (
    ('mon', 'Monday'),
    ('tu', 'Tuesday'),
    ('we', 'Wednesday'),
    ('th', 'Thursday'),
    ('fr', 'Friday'),
)

EVENT_TIME_SLOT = (
    ('10', '10:00'),
    ('13', '13:30'),
    ('17', '17:00'),
    ('20', '20:30'),
    ('00', '00:00'),
)

EVENT_TYPE = (
    ('2d', '2D'),
    ('3d', '3D'),
)

end_default = "for this Event was assigned yet"
days_default = "No day "
time_default = "No time "
hall_default = "No hall "

week_day = models.CharField(max_length=10, choices=EVENT_DAYS, default=days_default + end_default)
time_slot = models.CharField(max_length=10, choices=EVENT_TIME_SLOT, default=time_default + end_default)
type = models.CharField(max_length=2, choices=EVENT_TYPE, default=EVENT_TYPE[0])
hall = models.ForeignKey(Hall, on_delete=models.CASCADE)

film = models.ForeignKey(Movie, on_delete=models.CASCADE)

start_date = models.DateField(auto_now_add=False, auto_now=False, blank=True)
end_date = models.DateField(auto_now_add=False, auto_now=False, blank=True)

def __str__(self):
    return f'{self.film.name}'

我在这里试过这样的东西:

    default_sits_map = dict.fromkeys(hall.get_sits, 'Empty')

但是正如我所意识到的,您不能以这种方式访问​​ class(就像它是一个对象)。

我现在真的不明白如何根据大厅结构为每个事件创建这样的可更新变量。我提前感谢大家花时间阅读本文并就此主题发表意见。

更新:

这里也是函数的代码,它根据大厅名称(A,B或C)生成一个包含座位的列表

def generate_sits(_hall: str):
sits = []
row = int()

if _hall == 'A' or _hall == 'B' or _hall == 'C':
    if _hall == 'A':
        row = 14
    if _hall == 'B':
        row = 12
    if _hall == 'C':
        row = 9

    # sits setting begins here
    for i in range(row):
        i = i + 1
        sits.append("A" + str(i))
    for i in range(row):
        i = i + 1
        sits.append("B" + str(i))
    for i in range(row):
        i = i + 1
        sits.append("C" + str(i))
    for i in range(row):
        i = i + 1
        sits.append("D" + str(i))

    # if hall is "A" or "B" - they get more sits then "C"
    if _hall == 'A' or _hall == 'B':

        for i in range(row):
            i = i + 1
            sits.append("E" + str(i))
        for i in range(row):
            i = i + 1
            sits.append("F" + str(i))

return json.dumps(sits)

我通过稍微重组项目解决了这个问题。特定大厅的行和列现在只存储在模型中:

Models.py

class Hall(models.Model):
HALL_NAME = (
    ('A', 'Hall A'),
    ('B', 'Hall B'),
    ('C', 'Hall C')
)

hall_name = models.CharField(choices=HALL_NAME, max_length=30)
hall_sits = models.IntegerField()
hall_cols = models.IntegerField()

def __str__(self):
    return f'{self.hall_name}'

使用它,我能够从视图中生成一个新的事件地图 html/css 网格:

Views.py

def event_map(request, slug, event_id):
selected_event = Event.objects.get(pk=event_id)

movie = selected_event.film
hall = selected_event.hall
time = selected_event.get_time_slot_display()
date_event = selected_event.get_week_day_display()

sits = list(range(0, hall.hall_sits))
cols = list(range(0, hall.hall_cols))
# and so on

并将保留位置的检查移动到发票模型的描述中:

Models.py

class Invoice(models.Model):
user = models.ForeignKey(UserPersonalData, on_delete=models.CASCADE, 
blank=True, null=True)
total_price = models.FloatField()
email = models.CharField(max_length=320)
invoice_details = models.CharField(max_length=2048)
invoice_key = models.CharField(editable=False, max_length=12, default=generate_invoice_key())
event = models.ForeignKey(Event, on_delete=models.CASCADE)

def __str__(self):
    return f'{self.email} {self.invoice_key}'