[英]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)
null=False, blank=False
is the default and should be dropped. null=False, blank=False
是默认值,应该删除。company_name
and company_number
are related then you should make this 2nd Normal Form by making them a foreign key to a Company
modelcompany_name
和company_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)
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
表示如果您删除一个User
或UserProfile
,那么所有关联的Restaurants
也应该被删除。 Are you sure this is what you want?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()
。TextFields
with a length less than the max length for a CharField
.TextFields
的长度小于CharField
的最大长度是没有意义的。 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.我不确定您想对
name
和Rooms
字段做什么,所以我将跳过这些。 For the foreign key to Restaurant
:对于
Restaurant
的外键:
class Room(models.Model):
restaurant = models.ForeignKey(Restaurant, ...)
that allows you to say eg: my_room.restaurant.city
.这允许您说例如:
my_room.restaurant.city
。
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:对于您需要考虑的图像:
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.