Sahand
Sahand

Reputation: 8370

Referencing SQL model in mongoengine model field

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).

Upvotes: 0

Views: 59

Answers (1)

Jason
Jason

Reputation: 11363

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.

Upvotes: 1

Related Questions