0

I'm using mongoengine in my workout calendar Django project to store workouts with varying fields, depending on which lifts a user has done, how many of them, etc.

I have defined a Workout object that represents one workout done by one person at a certain date.

class Workout(Document):
    id = IntField(unique=True, null=False)
    date = DateTimeField()
    lifts = EmbeddedDocumentListField(LiftSerie)
    cardio = EmbeddedDocumentListField(CardioSerie)

Some of you might notice that there is no field representing the person who did the workout. That is where my question comes in. In my Django project, I have defined a custom User model that I've used instead of the normal django.contrib.auth.models.User object:

class User(AbstractUser):
    REQUIRED_FIELDS = []
    USERNAME_FIELD = 'email'
    email = models.EmailField(
        unique=True,
        },
    )

This model is a normal Django model, and is represented in an SQL database, SQLite in my case. I would like this model to act as the user field in my Workout mongoengine model. Something like this:

class Workout(Document):
    id = IntField(unique=True, null=False)
    date = DateTimeField()
    lifts = EmbeddedDocumentListField(LiftSerie)
    cardio = EmbeddedDocumentListField(CardioSerie)
    person = ReferenceField(User) #New row

Is this possible to achieve in some way? Or do I need to introduce redundancy to my web app by simply adding an email field to my mongoengine model to represent a user (email is a unique field in User).

Sahand
  • 7,980
  • 23
  • 69
  • 137

1 Answers1

1

I would implement a custom model manager that implements the query you're asking for by using db_manager

You're implementing a good amount of extra complexity by using two different database types. If I were you, I'd look at simplify your database infrastructure by leveraging the NoSQL and JSON capabilities of Postgres.

Jason
  • 11,263
  • 21
  • 87
  • 181
  • Thanks, Jason. Is this what you're talking about? https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/ – Sahand Dec 23 '17 at 14:28
  • 1
    correct, specifically [`JSONField`](https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#jsonfield) – Jason Dec 23 '17 at 14:32