简体   繁体   English

django 应用程序的数据库设计

[英]Database design for django application

I have a problem to make it easier : I have a relation like user -> Hotel - > Rooms .我有一个问题让它更容易:我有一个像user -> Hotel - > Rooms这样的关系。 User can create Hotel and then every hotel can have many rooms .用户可以创建酒店,然后每个酒店可以有很多房间。

Now I have a user model like现在我有一个像

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    company_name=models.CharField(max_length=50, null=False, blank=False)
    email=models.CharField(max_length=50, null=False, blank=False)
    company_number=models.CharField(max_length=50, null=False, blank=False)

and to create restaurant并创建餐厅

class Restaurant(models.Model):
    rest_owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name='rest_owner')
    name = models.CharField(max_length=50, null=False, blank=False)
    address = models.TextField(max_length=100, null=False)
    city = models.CharField(null=True, max_length=50)
    country = models.CharField(null=True, max_length=30)

and now room现在房间

class Room(models.Model):
    rest_owner = models.ForeignKey(Restaurant, on_delete=models.CASCADE, related_name='rest_owner')
    name = models.IntegerField()
    Rooms = models.IntegerField()

is that a good approach to do this ?这是做到这一点的好方法吗? and to save images of room and restaurant Should I make a并保存房间和餐厅的图像 我应该制作一个

class Image(models.Model):
    image = models.ImageField(null=False, upload_to='Gallery')

and then use in room and restaurant like images = models.ManyToManyField(Image) ?然后在房间和餐厅使用images = models.ManyToManyField(Image) . . I need to know best ways to doing both of these .我需要知道做这两个的最佳方法。 thanks谢谢

Most of your design is fine, however, there are a few minor issues:你的大部分设计都很好,但是,有一些小问题:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    company_name=models.CharField(max_length=50, null=False, blank=False)
    email=models.CharField(max_length=50, null=False, blank=False)
    company_number=models.CharField(max_length=50, null=False, blank=False)
  1. the User model already has an email field, so you don't need one here. User 模型已经有一个 email 字段,所以这里不需要一个。
  2. null=False, blank=False is the default and should be dropped. null=False, blank=False是默认值,应该删除。
  3. if company_name and company_number are related then you should make this 2nd Normal Form by making them a foreign key to a Company model如果company_namecompany_number相关,那么您应该通过使它们成为Company模型的外键来制作此第二范式
    class Company(models.Model):
        name = models.CharField(max_length=50)
        number = models.CharField(max_length=50)

    class UserProfile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        company = models.ForeignKey(Company)

For the Restaurant model:对于Restaurant模型:

class Restaurant(models.Model):
    rest_owner = models.ForeignKey(UserProfile, 
                                   on_delete=models.CASCADE, 
                                   related_name='rest_owner')
    name = models.CharField(max_length=50)
    address = models.TextField(max_length=100)
    city = models.CharField(null=True, max_length=50)
    country = models.CharField(null=True, max_length=30)
  1. The rest_owner foreign key says that one user can own multiple Restaurants , and the on_delete=CASCADE says that if you delete a User or UserProfile , then then all associated Restaurants should also be deleted. rest_owner外键表示一个用户可以拥有多个Restaurants ,而on_delete=CASCADE表示如果您删除一个UserUserProfile ,那么所有关联的Restaurants也应该被删除。 Are you sure this is what you want?你确定这是你想要的吗?
  2. The related_name makes it so you can say my_user.rest_owner.all() to get all the restaurants owned by the user, which doesn't exactly flow of the tongue.related_name使得它,所以你可以说my_user.rest_owner.all()来获取所有用户,这不正是流动舌拥有的餐馆。 It's better to use a plural of the class name you're in, ie related_name="restaurants" will give you my_user.restaurants.all() .最好使用您所在的类名的复数形式,即my_user.restaurants.all() related_name="restaurants"将为您提供my_user.restaurants.all()
  3. It doesn't make sense to have TextFields with a length less than the max length for a CharField .TextFields的长度小于CharField的最大长度是没有意义的。
  4. I would make country a two character field and fill it with the country's ISO 3166-1 alpha-2 code.我会让国家成为一个两个字符的字段,并用国家的 ISO 3166-1 alpha-2 代码填充它。

For the Room model:对于Room模型:

class Room(models.Model):
    rest_owner = models.ForeignKey(Restaurant, 
                                   on_delete=models.CASCADE, 
                                   related_name='rest_owner')
    name = models.IntegerField()
    Rooms = models.IntegerField()

I'm not sure what you want to do with the name and Rooms fields, so I'll skip those.我不确定您想对nameRooms字段做什么,所以我将跳过这些。 For the foreign key to Restaurant :对于Restaurant的外键:

  1. It is a good practice to name the foreign key field with the name of the model it points to, ie用它指向的模型的名称来命名外键字段是一个很好的做法,即
    class Room(models.Model):
        restaurant = models.ForeignKey(Restaurant, ...)

that allows you to say eg: my_room.restaurant.city .这允许您说例如: my_room.restaurant.city

  1. similarly to above, the related name should be a plural of the model you're in, so与上面类似,相关名称应该是您所在模型的复数形式,因此
    class Room(models.Model):
        restaurant = models.ForeignKey(Restaurant, related_name='rooms', ...)

which gives you my_restaurant.rooms.all() , which is reads much better than my_restaurant.rest_owner.all() -- which would be hard to guess returned rooms...这给了你my_restaurant.rooms.all() ,这比my_restaurant.rest_owner.all()读起来要好得多——这很难猜到返回的房间......

For the images you need to consider:对于您需要考虑的图像:

  1. can images be shared between rooms (yes => ManyToMany, no => ForeignKey)可以在房间之间共享图像吗(是 => 多对多,否 => 外键)
  2. if an image is edited, should all the rooms that use the image get the changes immediately (yes => ManyToMany, no => ForeignKey).如果图像被编辑,所有使用图像的房间都应该立即获得更改(是 => ManyToMany,否 => ForeignKey)。

a good rule-of-thumb though is - if you're in doubt, probably use a foreign key ;-)不过,一个很好的经验法则是 - 如果您有疑问,可以使用外键 ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM