0

Since Django is mapping each model to a table. I am not able to create packages in my code. Where I can wrap sections of the class to make it more logical and increase the coupling.

For example

class Employee(models.Model):

    #personal information
    first_name = models.CharField(max_length=20)
    middle_initial = models.CharField(max_length=1)
    last_name = models.CharField(max_length=20)
    dob = models.DateField()

    #job contract information 
    full_time = models.BooleanField()
    hours_per_day = models.PositiveSmallIntegerField()
    #.. and more

What I am trying to do is this

employee.job.is_full_time
employee.job.get_hours_per_day
# and more

However this means that I have to create a new model and connect it with the employee model using OneToOneField. I don't want to do that .. joins in the database are expensive. Is there anyway to create something like this ?

class Employee(models.Model):

    class PersonalInformation: 
        first_name = models.CharField(max_length=20)
        middle_initial = models.CharField(max_length=1)
        last_name = models.CharField(max_length=20)
        dob = models.DateField()

    class Job:
        full_time = models.BooleanField()
        hours_per_day = models.PositiveSmallIntegerField()
        #.. and more

The main key to the answer is to create a model that contain multiple classes which are going to be mapped to one table in the database.

2
  • Well, this model approach is not meant for mongodb/nosql, so you can't really nest the model like this. If you don't want to join, you must have one table, everything must be at the same level Commented Mar 27, 2014 at 7:21
  • @AntoinePelisse at least it should be a way to map multiple classes to one table. This is one of the basics in any system analysis and design. Commented Mar 27, 2014 at 10:03

1 Answer 1

2

There is no way to do that with just Django. Django models represent flat, relational databases. Extending the model framework to provide functionality beyond what its backend is capable of is unnecessary, and therefore not implemented in the default framework.

There are third-party packages that provide what you want, but as far as I know these packages use database backends that are capable of using such data structures.

Personally I would go with your first example, a flat, single model that represents all my Employee data. Prevent any disambiguity in your field names, and there will be no cost for using a flat model over a nested model.

And remember: premature optimization is a lot more expensive than an join statement.

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

1 Comment

+1 for using join. You should probably start by normalizing if it can remove some later anomalies (what if somebody has two jobs, etc)

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.