4
\$\begingroup\$

I have built a real estate management system with Django. I want to know if my design database was wrong. Please let me know how to improve it.

Should I make the models use more than two tables (classes)?

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)
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

As a rule of thumb in programming, "there are only four numbers": zero, one, two, and "many". You have seven photo fields, which means that you should just treat that as "many". Not only is that an unfortunate artificial limitation (someday there will be a listing with eight photos, and you won't be able to accommodate it easily), it's also a pain to write code that uses your schema (you either have to copy-and-paste code six times, or use variably-named identifiers).

What you want is a separate table for photos, to support a one-to-many relationship.

\$\endgroup\$
2
  • \$\begingroup\$ The rule I use is "a few" and "a lot" instead of two and many. But this is an excellent point! \$\endgroup\$
    – aghast
    Commented Apr 11, 2019 at 19:46
  • \$\begingroup\$ class Listing(models.Model): . . . . images = models.OneTwoManyField(Image) class Image(models.Model): photo = models.ImageField(upload_to='listings') , how is it? thanks for your suggestions \$\endgroup\$
    – namo
    Commented Apr 11, 2019 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.