Django - 房间模型需要选择多种房型。一个房间必须至少有一个预定义选择的房型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django - 房间模型需要选择多种房型。一个房间必须至少有一个预定义选择的房型相关的知识,希望对你有一定的参考价值。

房间没有保存到数据库,我该如何改进?有没有更好的办法?

一个房产可以有一个或多个房间。一个房间必须属于一个房产。

我首先创建房间模型

class Room(models.Model):
  property = models.ForeignKey(Property)

  name = models.CharField(max_length=250)

  description = models.TextField(max_length=800)

  image = models.ImageField(upload_to='room_images/', blank=False)

  bar_rate = models.IntegerField(default=0, blank=False)

  max_occupancy = models.IntegerField(default=1, blank=False)

  extra_beds = models.IntegerField(default=0, blank=True)

  price_per_exra_bed = models.IntegerField(default=0, blank=True)

  is_breakfast_included = models.BooleanField(default=False)

  room_type_quantity = models.IntegerField(default=0, blank=False)

  ROOM_TYPES = (
    ('SGL', 'Single'),
    ('TWN', 'Twin'),
    ('DBL', 'Double'),
    ('TRPL', 'Triple'),
    ('STND', 'Standard'),
    ('DLX', 'Deluxe'),
    ('EXET', 'Executive'),
    ('SPR', 'Superior'),
    ('JS', 'Junior Suite'),
    ('ONEBDR', 'One Bedroom'),
    ('TWOBDR', 'Two Bedroom'),
    ('THREEBDR', 'Three Bedroom'),
    ('FOURBDR', 'Four Bedroom'),
    ('FIVEBDR', 'Five Bedroom'),
    ('SIXBDR', 'Six Bedroom'),
    ('SEVENBDR', 'Seven Bedroom'),
   )
  room_type = models.CharField(max_length=1, choices=ROOM_TYPES, default="STND")


def __str__(self):
  return self.name

我创建了一个新表单,允许用户填写表单。在forms.py中

class RoomForm(forms.ModelForm):
  class Meta:
    model = Room
    exclude = ("property",)

如您所见,我排除了属性模型,我不需要将属性信息添加到RoomForm中。

在views.py中,我修改了property_add_room函数以将空间保存到数据库中:

@login_required(login_url='/property/sign-in/')
def property_add_room(request):
  form = RoomForm()

  if request.method == "POST":
    form = RoomForm(request.POST, request.FILES)

    if form.is_valid():
      room = form.save(commit=False)
      room.property = request.user.property
      room.save()
      return redirect(property_room)

  return render(request, 'property/add_room.html', {
     "form": form
  })

这是房产模型。用户有一个属性,一个属性必须属于一个用户。

class Property(models.Model):
 user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='property')
 name = models.CharField(max_length=500)
 phone = models.CharField(max_length=500)
 address = models.CharField(max_length=500)
 total_rooms = models.CharField(max_length=500)
 logo = models.ImageField(upload_to='property_logo/', blank=False)
答案

您应该更改您的房间模型。如果用户被删除,您的财产也将被删除。你确定要这个吗?

class Room(models.Model):
  property = models.ForeignKey(Property)
  name = models.CharField(max_length=250)
  description = models.TextField(max_length=800)
  image = models.ImageField(upload_to='room_images/', blank=False)
  bar_rate = models.IntegerField(default=0, blank=False)

  SGL = 1
  TWN = 2
  DBL = 3
  TRPL = 4
  STND = 5
  DLX = 6
  EXET = 7
  SPR = 8
  SPR = 9
  JS = 10
  ONEBDR = 11
  TWOBDR = 12
  THREEBDR = 13
  FOURBDR = 14
  FIVEBDR = 15
  SIXBDR = 16
  SEVENBDR = 17

  ROOM_TYPES = (
    (SGL, 'Single'),
    (TWN, 'Twin'),
    (DBL, 'Double'),
    (TRPL, 'Triple'),
    (STND, 'Standard'),
    (DLX, 'Deluxe'),
    (EXET, 'Executive'),
    (SPR, 'Superior'),
    (JS, 'Junior Suite'),
    (ONEBDR, 'One Bedroom'),
    (TWOBDR, 'Two Bedroom'),
    (THREEBDR, 'Three Bedroom'),
    (FOURBDR, 'Four Bedroom'),
    (FIVEBDR, 'Five Bedroom'),
    (SIXBDR, 'Six Bedroom'),
    (SEVENBDR, 'Seven Bedroom'),
  )
  room_type = models.IntegerField(choices=ROOM_TYPES)
  max_occupancy = models.IntegerField(default=1, blank=False)
  extra_beds = models.IntegerField(default=0, blank=True)
  price_per_exra_bed = models.IntegerField(default=0, blank=True)
  is_breakfast_included = models.BooleanField(default=False)
  room_type_quantity = models.IntegerField(default=0, blank=False)

  def __str__(self):
    return self.name

以上是关于Django - 房间模型需要选择多种房型。一个房间必须至少有一个预定义选择的房型的主要内容,如果未能解决你的问题,请参考以下文章

华住酒店会员怎样用积分兑换房间

Spring Data和Hibernate中不受控制的删除

Java/Javascript/JSP 中的下拉选项

如何把预定房取消

学习心得3

预定房台并给出标识