Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6019489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:25:45+00:00 2026-05-23T03:25:45+00:00

I am trying out MongoEngine , a DRM library for Python to use with

  • 0

I am trying out MongoEngine, a DRM library for Python to use with MongoDB. I can define a model like this example from the site:

class User(Document):
    email = StringField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)

It works like a charm and MongoEngine is really nice. Well, I want to go further and wonder if I can define my models somewhere in a file in JSON or YAML, or whatever so that then create a model using it. So this declaration could look like this in JSON:

{
    "model":"User",
    "fields":{
        "email":{
            "type":"string",
            "required":"true"
        },
        "first_name":{
            "type":"string",
            "max_length":"50"
        },
        "last_name":{
            "type":"string",
            "max_length":"50"
        }
    }
}

Then I would parse this JSON and create a model using it. May be it could be just a single import operation that I would perform each time I modify the model definition, or may be it could parse the whole JSON every time. Is that a good scenario? I just want to let people who will use the app define their own models without having to dig into the code. Any ideas how to do dynamic model creation are appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:25:46+00:00Added an answer on May 23, 2026 at 3:25 am

    If you’re going to use YAML, pyyaml is completely painless, and automatically outputs a data structure using python’s built-in types (or even more complex types that you define).

    Any way you go, I would also highly recommend Rx as a validator so you can easily verify the integrity of the loaded files.*

    As for using this to create a model you can use the built-in function type (not type(object), but type(name, bases, dict)) which… “[r]eturn a new type object. This is essentially a dynamic form of the class statement.”

    So, you can call:

    def massage(fields_dict):
        #transform your file format into a valid set of fields, and return it
    
    user_class = type(yaml_data['model'], Document, massage(yaml_data['fields']) )
    

    *I’ve used both of these together in the last eight hours, coincidentally – they work together painlessly, e.g.:

    import yaml
    import Rx
    
    data = yaml.load(open("foo.yaml")
    rx = Rx.Factory({ "register_core_types": True })
    schema = rx.make_schema(yaml.load(open("schema.yaml")))
    
    if not schema.check(data):
        raise ValueError("data file contents are not in a valid format")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying out the example given in http://docs.python.org/library/re.html for the group numbering and
I'm trying out the example Hello World from this link here: http://www.mulesoft.org/documentation/display/MULE2INTRO/Quick+Start I have
I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%'
Trying out Linq for NHibernate and first noticed this: var list1 = (from ut
I'm currently trying out db4o (the java version) and I pretty much like what
I have been trying out the NameValueDeserializer from MVCContrib, which will take a IList
I'm trying out http://emacspeak.sourceforge.net now that I have it running on windows. I'd like
Am trying out Ruby on Rails in Windows XP using the Hello World example.
I'm trying out the Razor ViewEngine from ASP.NET MVC 3 Preview 1 and I'm
I am trying out some globalization in an asp.net mvc2 application but can't get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.