在django模式下自动生成座位

时间:2017-04-23 12:52:00

标签: python django database sqlite

我正在使用Django开发一个巴士预订网站。我在不同的Django应用程序中有两个模型文件。其中一个型号是巴士,另一个是座位。总线类中有容量整数字段。我希望在数据库中创建总线时,自动循环运行并创建等于总线类容量的座位。也许在查看我的模型文件后你会得到一个更清晰的视图。

  

\ SRC \书\ 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 \网络\ 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中创建一个频率类来添加总线的频率。如果我创建一个日期时间字段,那么我将不得不一次又一次地重复每个总线对象,因为每个日期都不是很方便。所以欢迎任何建议。如果您还需要查看其他任何文件,请与我们联系。感谢。

1 个答案:

答案 0 :(得分:1)

在我看来,针对您的问题,最优雅的解决方案是django post_save signal

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( )

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