gpt4 book ai didi

python - 如何在Django模型中定义关系?

转载 作者:行者123 更新时间:2023-12-01 06:38:48 26 4
gpt4 key购买 nike

我正在编写一个 Django 应用程序,用户可以在其中创建有关可用房间出租的帖子,并附有描述和图片。

我很难弄清楚如何定义模型中的关系。我认为用户类和地主类之间的关系将是多对一的关系。我的问题是,如何定义用户、房东和其他类(如rentalProperty、契约(Contract)和媒体)之间的关系。

谢谢。

from django.db import models
from django.contrib.auth.models import User

# Create your models here.


class Landlord(models.Model):
user = models.ForeignKey("User", on_delete=models.PROTECT)
address = models.CharField(max_length=255)
social_security_number = models.CharField(max_length=255)
email_address = models.CharField(max_length=255)


class rentalProperty(models.Model):
landlord = models.ForeignKey("Landlord", on_delete=models.PROTECT)
PROPERTY_LISTING_CHOICES = [
(APARTMENT, 'Apartment'),
(HOLIDAY_HOME, 'Holiday home'),
(SINGLE_FAMILY_HOME, 'Single family home'),
(COMMERCIAL, 'Commercial'),
]
type_of_property_listing = models.CharField(
max_length = 50,
choices = PROPERTY_LISTING_CHOICES,
default = APARTMENT
)
BUILDING_LISTING_CHOICES = [
(CONDOMINIUM, 'Condominium'),
(ROW_HOUSE, 'Row house'),
(SINGLE_FAMILY_HOUSE, 'Single family house'),
(HOLIDAY_HOME, 'Holiday_home'),
(DUPLEX, 'Duplex'),
(SINGLE_FAMILY_HOME_COOP, 'Single family home(CO-OP)'),
(CONDOMINIUM_EXTERNAL_ENTRANCE, 'Condominium external entrance'),
(WOODEN_COMDOMINIUM_HOUSE, 'Wooden condominium house')
]
type_of_building_choices = models.CharField(
max_length = 50,
choices = BUILDING_LISTING_CHOICES,
default = CONDOMINIUM
)
created_by = models.ForeignKey(User, related_name='rentalProperties')

class Location(models.Model):
rental_property = models.ForeignKey("rentalProperty", on_delete=models.PROTECT)
street = models.CharField(max_length=255)
borough = models.CharField(max_length=255)
postal_code = models.CharField(max_length=20)
city = models.CharField(max_length=50)
province = models.CharField(max_length=50)
country = models.CharField(max_length=50)

class ApartmentBasicInfo(models.Model):
location = models.ForeignKey("Location", on_delete=models.PROTECT)
title = models.TextField()
living_area = models.IntegerField()
number_of_rooms = models.IntegerField()
floors = models.IntegerField()
furnished = models.BooleanField(initial=True)
KITCHEN_TYPE_CHOICES = [
(UNKNOWN, 'Unknown'),
(NO_KITCHEN, 'No kitchen'),
(KITCHEN, 'Kitchen'),
(MINI_KITCHEN, 'Mini kitchen'),
(OPEN_KITCHEN, 'Open kitchen')
]
type_of_kitchen_choices = models.CharField(
max_length = 50,
choices = KITCHEN_TYPE_CHOICES,
default = UNKNOWN
)
sauna = models.BooleanField(initial=True)
balcony = models.BooleanField(initial=True)
apartment_layout_type = models.IntegerField()
APARTMENT_CONDITIONS_CHOICES = [
(EXCELLENT, 'excellent'),
(GOOD, 'good'),
(FAIR, 'fair'),
(POOR, 'poor'),
(VERY_BAD, 'very bad')
]
apartment_condition = models.CharField(
max_length = 50,
choices = APARTMENT_CONDITIONS_CHOICES,
default = EXCELLENT
)
apartment_conditions_freetext = models.TextField()




class Contract(models.Model):
rentalproperty = models.ForeignKey("rentalProperty", on_delete=models.PROTECT)
RENTAL_TERMS_CHOICES = [
(PERMANENT, 'permanent'),
(FiXED, 'fixed'),
]
type_of_rental_terms = models.CharField(
max_length = 20,
choices = RENTAL_TERMS_CHOICES,
default = PERMANENT
)
rent_availability = models.DateField()
rent_availability_info = models.TextField()
rent_per_month = models.IntegerField()
deposit_for_rent = models.IntegerField()
deposit_for_rent_info = models.TextField()
rental_increment = models.IntegerField()
index_value = models.IntegerField()
index_date = models.DateField()
RENT_ELECTRICITY_CHOICES = [
(UNDEFINED, 'undefined'),
(INCLUDED_IN_THE_RENT, 'included in the rent'),
(RESPONSIBILITY_OF_TENANT, 'responsibility of tenant'),
(FIXED_FEE, 'fixed fee'),
(OTHER, 'other')
]
rent_electricity = models.CharField(
max_length = 50,
choices = RENTAL_TERMS_CHOICES,
default = UNDEFINED
)
electricity_fixed_fee = models.IntegerField()
WATER_FEE_CHOICES = [
(UNDEFINED, 'undefined'),
(INCLUDED_IN_THE_RENT, 'included in the rent'),
(RESPONSIBILITY_OF_TENANT, 'responsibility of tenant'),
(FIXED_FEE_PER_PERSON, 'fixed fee per person'),
(OTHER, 'other')
]
water_fee = models.CharField(
max_length = 50,
choices = WATER_FEE_CHOICES,
default = UNDEFINED
)
water_fee_per_person = models.IntegerField()
HEATING_FEE_CHOICES = [
(UNDEFINED, 'undefined'),
(INCLUDED_IN_THE_RENT, 'included in the rent'),
(RESPONSIBILITY_OF_TENANT, 'responsibility of tenant'),
(FIXED_FEE, 'fixed fee'),
(OTHER, 'other')
]
heating_fee = models.CharField(
max_length = 50,
choices = HEATING_FEE_CHOICES,
default = UNDEFINED
)
heating_fixed_fee = models.IntegerField()
Other_cost = models.TextField()
pets_allowed = models.BooleanField(initial=True)
smoking_allowed = models.BooleanField(initial=True)
insurance_required = models.BooleanField(initial=True)
other_terms = models.TextField()

class Media(models.Nodel):
apartment_pictures = models.ImageField()
videos = models.CharField(max_length=512)
virtual_tours = models.CharField(max_length=512)

最佳答案

您的示例中有很多..很多错误。我认为这只是一个草稿。

我想展示一些修复和建议。

  1. 在模型中,您应该使用 default而不是initial -initial正在使用 Forms .
  2. 对于choices我想展示我如何解决choices以更简单的方式 - 只需使用 enum
  3. 使用OneToOne时关系,就像我的例子 LocationRentalProperty.location 返回的对象是 Location 的一个实例类而不是 QuerySet - 在代码中更容易处理
from django.contrib.auth import get_user_model
from django.db import models
import enum


UserModel = get_user_model()


class ToChoicesEnum(enum.Enum):
@classmethod
def to_choices(cls):
return tuple((tag.name, str(tag.value)) for tag in cls)


class Landlord(models.Model):
user = models.ForeignKey(UserModel, on_delete=models.PROTECT)
address = models.CharField(max_length=255)
social_security_number = models.CharField(max_length=255)
email_address = models.EmailField()


class RentalProperty(models.Model):
landlord = models.ForeignKey("Landlord", on_delete=models.PROTECT)
created_by = models.ForeignKey(UserModel, related_name='rentalProperties')

class PropertyListingChoices(ToChoicesEnum):
APARTMENT = 'Apartment'
HOLIDAY_HOME = 'Holiday home'
SINGLE_FAMILY_HOME = 'Single family home'
COMMERCIAL = 'Commercial'

type_of_property_listing = models.CharField(
max_length=50,
choices=PropertyListingChoices.to_choices(),
default=PropertyListingChoices.APARTMENT
)
...


class Location(models.Model):
rental_property = models.OneToOneField("RentalProperty", related='location', on_delete=models.PROTECT)
...


class ApartmentBasicInfo(models.Model):
rental_property = models.OneToOneField("RentalProperty", related='basic_info', on_delete=models.PROTECT)
...


class RentalImages(models.Model):
rental_property = models.ForeignKey("RentalProperty", related='images', on_delete=models.PROTECT)
apartment_picture = models.ImageField()


class RentalMedia(models.Model):
class AssetTypeChoices(ToChoicesEnum):
VIDEO = 'video'
TOUR = 'tour'
rental_property = models.ForeignKey("RentalProperty", related='rental_media', on_delete=models.PROTECT)
asset_url = models.URLField()
asset_type = models.CharField(max_length=10, choices=AssetTypeChoices.to_choices())


class Contract(models.Model):
rental_property = models.ForeignKey("RentalProperty", related='contracts', on_delete=models.PROTECT)
user = models.ForeignKey(UserModel, related='contracts', on_delete=models.PROTECT)
smoking_allowed = models.BooleanField(default=True) # in models you should use default instead initial
...

关于python - 如何在Django模型中定义关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com