Search code examples
djangopostgresqlforeign-key-relationship

Django: Parent table having multiple inline tables linked using foreignkey from parent to each inline table


I am creating a set of models in a way that certain models can contain details from multiple parent classes. My project requires me to have these main classes:

-- {class name} {file name they are contained in}
-- Attraction (attraction.py)
-- Tours (tours.py)
-- Transit (transit.py)
-- Flight (flight.py)
-- Accomodation (accomodation.py)

Each one of them has multiple common fields, which are modelled into common models ; I want to have foreign key from the classes above to the ones below:

-- Contact (contact.py)
-- Address (address.py)
-- LocationMetadata (location.py)
-- Duration (duration.py)
-- Price (price.py)

I made foreign key from

attraction --> contact, address, location, price

transit --> location, price

accomodation --> location, type, price, contact, address

Example Model Atraction :

class Attraction(models.Model):
    ...

    ATTRACTION_TYPE = (
       ('NIGHT-LIFE', 'NL'),
       ('EARTERY', 'EAT'),
       ('CULTURAL', 'CUL'),
       ('LANDMARK', 'LNDMRK'),
       ('ADVENTURE', 'ADV'),
       ('ENTERTAINMENT', 'ENT'),
       ('SCENIC', 'SNC'),
       ('WILDLIFE', 'WDL')
    )

    type = forms.MultipleChoiceField(choices=ATTRACTION_TYPE)
    ...

I want to create the admin access to have ( attraction, transit, accomodation) as access points and rest to be inline to them. But this is not permitted in Django... is there a way to go around it???


Solution

  • This proved to be the best solution for what i needed to do.

    https://pypi.python.org/pypi/django-multiselectfield
    

    It provides a multiselect field inside model itself; and adds all the selected entries as csv's .