简体   繁体   English

Model 链接到另外两个模型。 如何访问 django 中其他两个模型的属性

[英]A Model is linked to two other models. How do I access the properties of that two other models in django

I am in a process of creating a Room Booking Management System using Django. I have faced an issue in accessing models.我正在使用 Django 创建客房预订管理系统。我在访问模型时遇到了问题。

Here is my Room Model这是我的房间 Model

class Room(models.Model):
name = models.CharField(max_length=30)
date =  models.DateField()
defined_check_in_time =  models.IntegerField()
defined_check_out_time = models.IntegerField()
booked = models.BooleanField(default = False)

USERNAME_FIELD = 'name'

class Meta:
    ordering = ['date', 'defined_check_in_time']

def __str__(self):
    return self.name

def is_booked(self):
    return self.booked

def set_booked(self):
    self.booked = True

Here is my Booking Model这是我的预订 Model

class Booking(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
room = models.ForeignKey(Room, on_delete=models.CASCADE)

def __str__(self):
    return f'{self.user} has booked {self.room} from {self.room.defined_check_in_time} to {self.room.defined_check_out_time} on {self.room.date}'

I have linked the User model and Room model in the Booking model using a foreign key when a user books a room.当用户预订房间时,我使用外键将预订 model 中的用户 model 和房间 model 链接起来。 User model is defined in another file.用户 model 在另一个文件中定义。 I have not included that model code here.我没有在此处包含该 model 代码。

When a user books a room, the corresponding room object and user object is linked to the booking object. To display the bookings of the user, I need to query the Booking model using a User object.当用户预订房间时,对应的房间object和用户object链接到预订object。为了显示用户的预订,我需要使用用户object查询预订model。

Here, my question is how i can access user and room object attributes inside the booking object?在这里,我的问题是如何访问预订 object 中的用户和房间 object 属性?

To access information within your booking object, you must first select a booking object, whichever way you choose to identify a booking.. maybe by an id.要访问您的预订 object 中的信息,您必须首先 select 预订 object,无论您选择哪种方式来识别预订……可能是通过 ID。 Let's say booking id=100假设预订 ID = 100

booking = Booking.objects.get(id=100)

Now to find foreign key info you can just.现在要查找外键信息就可以了。 lookup.抬头。

booking_user = booking.user.username booking_room = booking.room.name booking_user = booking.user.username booking_room = booking.room.name

or to find any bookings on rooms booked:或查找已预订房间的任何预订:

booked_rooms = Booking.objects.filter(room.booked == True)

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

相关问题 我有两个模型,我想从Company模型中访问Floorsheet模型的第一个和最后一个“比率”。 我如何在Django模型中做到这一点? - I have two models and i want to access first and last 'rate' of Floorsheet models from Company models. How can i do it in django models? django模型与其他两个模型的关系 - django model relationship to two other models 如何在 Django 中检查两个模型是否相等? - How can I check if two models equal each other in Django? 需要快速更新Django模型与其他两个模型之间的差异 - Need to quickly update Django model with difference between two other models 在Django中连接3个模型以访问其他模型数据 - Connect 3 models in Django to access other model data 如何让两个模型互相引用Django - How to have two models reference each other Django Django 模型。 如何在 Django 中创建“添加新”文件? - Django models. How do I make "add new" filed in Django? 如何检查 Django 中来自不同模型的两个对象是否彼此相等? - How can I check if two objects from differente models equal each other in Django? 如何将两个 keras 型号连接到一个 model 中? - How do I connect two keras models into one model? 具有Django模型,该模型可以属于其他两个模型之一(外键关系) - Having a django model which can belong to either of two other models (foreign key relation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM