1

I am trying to build a tool that, at a simple level, tries to analyse how to buy a flat. DB = POSTGRES

So the model basically is:

class Property(models.Model):
    address = CharField(max_length = 200)   
    price = IntegerField()
    user = ForeignKey(User)     # user who entered the property in the database
    #..
    #..
    # some more fields that are common across all flats



    #However, users might have their own way of analysing 

    # one user might want to put

    estimated_price = IntegerField() # his own estimate of the price, different from the zoopla or rightmove listing price
    time_to_purchase = IntegerField()  # his own estimate on how long it will take to purchase


    # another user might want to put other fields
    # might be his purchase process requires sorting or filtering based on these two fields

    number_of_bedrooms = IntegerField()
    previous_owner_name = CharField()       

How do I give such flexiblity to users? They should be able to sort , filter and query their own rows (in the Property table) by these custom fields. The only option I can think of now is the JSONField Postgres field

Any advice? I am surprised this is not solved readily in Django - I am sure lots of other people would have come across this problem already

Thanks

1 Answer 1

1

Edit: As the comments point out. JSON field is a better idea in this case.

Simple. Use Relations.

Create a model called attributes.

It will have a foreign key to a Property, a name field and a value field.

Something like,

class Attribute(models.Model):
    property = models.ForiegnKey(Property)
    name = models.CharField(max_length=50)
    value = models.CharField(max_length=150)

Create an object each for all custom attributes of a property.

When using database queries use select_related of prefetch_related for faster response, less db operations.

Sign up to request clarification or add additional context in comments.

2 Comments

This is known as Entity Attribute Value, or EAV modelling. Using a JSONField is a better option than this, you can index columns of a JSONField if they become popular.
We've done a few EAV implementations with relations. It works, it scales, but when it gets really big it tends to get overwhelming. We've started working on a JSON solution: github.com/zostera/django-jeaves

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.