【问题标题】:design django model for real estate为房地产设计 django 模型
【发布时间】:2019-09-01 11:51:49
【问题描述】:

我已经用 django 构建了一个房地产管理系统。我想知道我的设计数据库是否有问题,请告诉我以改进它

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    image = models.ImageField(upload_to='profiles', default='logo.png')
    phone = models.CharField(max_length=11, default='')
    bio = models.CharField(max_length=100, default='')
    city = models.CharField(max_length=20, default='erbil')
    location = models.CharField(max_length=40, default='')
    date = models.DateTimeField(default=datetime.now)
    active = models.BooleanField(default=False)

    def __str__(self):
        return f'{self.user}'


class Listing(models.Model):
    objects = ListingManager()

    company = models.ForeignKey(User, on_delete=models.CASCADE )
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True, default='', blank=True)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100, choices=city_choices, default='lodon')
    estate_type = models.CharField(max_length=20, choices=estate_choices, default=house)
    description = models.TextField(blank=True)
    rent_sale = models.CharField(max_length=20, choices=rent_sale_choice, default=sale)
    price = models.IntegerField()
    bedrooms = models.IntegerField(default=0)
    bathrooms = models.DecimalField(max_digits=2, decimal_places=1)
    garage = models.IntegerField(default=0)
    sqft = models.IntegerField()
    sold =  models.BooleanField(default=False)
    sold_time = models.DateTimeField(default=datetime.now,blank=True)
    photo_main = models.ImageField(upload_to='listings_main')
    photo_1 = models.ImageField(upload_to='listings_1', blank=True)
    photo_2 = models.ImageField(upload_to='listings_1', blank=True)
    photo_3 = models.ImageField(upload_to='listings_1', blank=True)
    photo_4 = models.ImageField(upload_to='listings_1', blank=True)
    photo_5 = models.ImageField(upload_to='listings_1', blank=True)
    photo_6 = models.ImageField(upload_to='listings_1', blank=True)
    is_published = models.BooleanField(default=True)
    list_date = models.DateTimeField(default=datetime.now, blank=True)
    unless = models.CharField(default='', max_length=20)

我应该更改代码吗?如果是的话应该在哪里改变?或者为两个以上的表(类)制作模型

【问题讨论】:

  • 对我来说你的模型没问题或者你对数据有什么问题吗?
  • 您的问题最好在codereview 网站上提出
  • 照片可能位于带有外键的单独模型中
  • 类列表(models.Model):。 . . . images = models.OneTwoManyField(Image) class Image(models.Model): photo = models.ImageField(upload_to='listings') ,怎么样?

标签: django database model


【解决方案1】:

对于模型中的时间戳,使用 django utils TimeStampedModel

以下是我的建议:

对于手机归档使用https://github.com/stefanfoulis/django-phonenumber-field,它将为您节省大量验证和时间

如 Django 文档建议的那样,choices 变量应为大写

将 PositiveIntegerField() 用于价格、卧室和任何有意义的积极模型

为什么浴室 = models.DecimalField() ?

用外键将不同模型中的照片与列表分开

【讨论】:

  • 如果没有淋浴/浴缸,浴室有时被指定为 1/2。
猜你喜欢
  • 2019-11-30
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 2018-06-10
相关资源
最近更新 更多