Django模式下的自动座椅



我正在使用Django开发一个总线预订网站。我在不同的Django应用中有两个模型文件。其中一个是公共汽车,另一个是座位。公共汽车课上有容量Integerfield。我希望在数据库中创建总线时,会自动运行循环并创建等于总线类容量的座椅。也许在查看我的模型文件后,您会得到更清晰的视图。

src book models.py

from django.db import models
from django.contrib.auth.models import User
from web.models import Bus, Route
class Booking(models.Model):
    class Meta:
        verbose_name_plural = "Booking"
    user = models.ForeignKey(User)
    #session = models.ForeignKey(sessions.Sessions)
    name = models.CharField(max_length=50)
    gender = models.CharField(max_length=10, choices=(('Mr', 'mr'), ('Mrs', 'mrs'), ('Ms', 'ms'),))
    age = models.IntegerField()
    email = models.EmailField()
    phone = models.IntegerField()
    bus = models.ForeignKey(Bus, default='')
    def __str__(self):
        return self.name
class Seat(models.Model):
    class Meta:
        verbose_name_plural = "Seat"
    for seats in range(1,int(float(Bus.capacity)+1.0)):   
        id = models.AutoField(primary_key=True)
        bus = models.ForeignKey(Bus)
    def __str__(self):
        return str(self.id)

class Ticket(models.Model):
    class Meta:
        verbose_name_plural = "Ticket"
    seat = models.ForeignKey(Seat)
    bus = models.ForeignKey(Bus)
    route = models.ForeignKey(Route, default='')
    def __str__(self):
        return str(self.id)

src web models.py

from django.db import models
from django.core.exceptions import ValidationError

class Route(models.Model):
    class Meta:
        verbose_name_plural = "Routes"
    BUSFROM = (
    ('Delhi', 'Delhi'),
    ('Jaipur', 'Jaipur'),
    ('Ajmer', 'Ajmer'),
    )
    BUSTO = (
    ('Ajmer', 'Ajmer'),
    ('Chandigarh', 'Chandigarh'),
    ('Delhi', 'Delhi'),
    )
    route_id = models.AutoField(primary_key=True,)    
    location_from = models.CharField(max_length=255, choices=BUSFROM)
    location_to = models.CharField(max_length=255,choices=BUSTO)    
    route_name = models.CharField(max_length=500, default='', editable=False)

    def __str__(self):
        if self.location_from == self.location_to:
            raise ValidationError('To and From Can't be the same')
        self.route_name = '{0}-{1}'.format(str(self.location_from), str(self.location_to))
        return self.route_name

class Bus(models.Model):
    BUSTYPE = (
    ('Volvo', 'Volvo'),
    ('Ordinary', 'Ordinary'),
    )
    class Meta:
        verbose_name_plural = "Bus"
    type_of_bus = models.CharField(max_length=255, choices=BUSTYPE)
    bus_registration = models.CharField(max_length=255, default='')
    capacity = models.IntegerField(default=0)
    bus_number = models.IntegerField()
    route = models.ForeignKey(Route,)

    def __str__(self):
        return '{0}, {1}, {2}'.format(str(self.bus_number), self.type_of_bus, self.route)

您可以在书中 models.py中看到,由于明显的递延对象的原因,For循环失败了。

另外,我想在Web models.py中创建一个频率类,以添加总线的频率。如果我进行DateTime字段,那么我将不得不一次又一次地重复每个日期,这不是很方便。因此,欢迎任何建议。让我知道您是否需要查看任何其他文件。谢谢。

我认为,最优雅的解决方案是django postrongave信号。

from django.db.models.signals import post_save
from django.dispatch import receiver
class Bus(models.Model):
    # ... fields here
class Seat(models.Model):
    id = models.AutoField(primary_key=True)
    bus = models.ForeignKey(Bus)
    # ... 
# function to create seats
@receiver(post_save, sender=Bus)
def create_seats(sender, instance, created, **kwargs):
    if created:
        for seat in range (0, instance.capacity):
            instance.seat_set.create( )

另一个解决方案是覆盖总线保存方法。

最新更新