I have a class in my existing python project, User, that I would like to map to tables. But I'm not sure what the best way to do this is?
Does this mean I can remove:
class User:
pass
from my model/__ init __.py?
Or should I leave that in there, and have something like:
from project.model.user import User
class User:
pass
In essence, having (2) different classes with the same name?
Thanks.
You should not define a class that maps onto a table in the model's __init__.py file, nor should you have two different clases with the same name.
Classes that map onto tables belong in your project's model directory, grouped into modules. Then import the classes in __init__.py to make them available.
Related
I have an issue where there are two sets of users for one set of MongoEngine documents. One of these uses sets wants complex helper functions to be part of the document definition, the other user set cannot install the dependencies that the those functions require.
I want to achieve something like a clean model and a dirty model where the user can select for themselves, but the actual database document definition is maintained in only one place. E.g.
CleanModel/my_document.py
class MyDocument(Document):
my_field = StringField()
DirtyModel/my_document.py
from CleanModel import MyDocument as _MyDocument
class MyDocument(_MyDocument):
def some_function_that_relies_on_compiled_code():
pass
The use could then do:
from DirtyModel import MyDocument
or
from CleanModel import MyDocument
and work with exactly the same Mongo document. The way I've written it above, DirtyModel.MyDocument.objects() can't get at documents created with the CleanModel.
Any ideas on the best way to solve this? (I don't think that moving the helper methods outside of the document definition is practical in this case)
There are instance-based functions and instance variables that I need to call from a custom Django migrations file. Can I do this, and if so, how?
For example I have an instance, c, of class Car. How do I call c.honk() from within a migrations file, or access c.colour ?
P.S.
I know that I can 'import' models within the migrations file using ModelName = apps.get_model('appname', 'ModelName') , and I know that I can call class-based functions by doing import('appname').pythonfile.functionname . In other words I know how to do things like call Car.get_components() from within a migrations file.
P.P.S. I'm not sure whether "instance-based functions" and "class-based functions" is the right terminology, but I hope you understand what I mean.
As per Django documentation you can't access arbitrary code (custom methods, __init__, save etc.) in migrations, however you can use custom managers if they have use_in_migrations = True attribute.
I am aware that you can get a reference to an existing model from within another model by using self.pool.get('my_model')
My question is, how can I get a reference to a model from a Python class that does NOT extend 'Model'?
In that case you will have to import the class like a normal Python class, writing in your .py file:
from your_path_class import YourClass
And then you will be able to work with it:
Yourclass.any_method()
By the way, self.pool.get('your model') in Odoo 8 API is self.env['your_model'].
It's pretty basic and simple any python class can be called from it's name space, so call your class from namespace and instanciate the class.
Even Model class or any class inherited from Model can be called and instanciated like this.
Self.pool is just orm cache to access framework persistent layer.
Bests
I'm going to ask this question in two parts: first the general question, and than the question for my specific use case.
The general question:
I'm building a podcast app, where, hopefully, we'll have users. Users have subscripitons, settings, ... , which I'd like to store on the User object, but subscriptions and settings don't belong in the same module in my code.
How do you structure your code so that all the relevant data about a user is stored together, but the code that defines and deals with specific properties can be separated?
My specific use case
I'm building the back end on Google App Engine. My user class looks something like this:
class User(ndb.Model):
username = ndb.StringProperty(required=True)
email = ndb.StringProperty(required=True)
...
Now I could just add another property for subscriptions, settings etc, but these definitions don't really belong in the users module. I've tried defining a SubscriptionsHolder and SettingsHolder class using ndb.PolyModel, but with multiple inheritance, only queries on the last superclass in the User definition supports querying.
I could just make the settings and other module query the User model directly, but this results in a circular dependency, where the User the users module depends on settings for subclassing, and settings depends on users for querying. I know I can resolve the circular dependency by moving the import statement around, but that just seems like a hack to me.
My approach was to treat User and Settings data as separate but related collections. Instead of subclassing or using PolyModel I simply introduced a way to imply a 1:1 relation between those data sets.
One way is to add a KeyProperty to Settings that links back to User. Another way is to create each Settings entity with the same id/name that is used by the related User entity. This second way allows a direct Settings.get_by_id() call once you have the User key.
Does anyone has any insight on organizing sqlalchemy based projects? I have many tables and classes with foreign keys, and relations. What is everyone doing in terms of separating classes, tables, and mappers ? I am relatively new to the framework, so any help would be appreciated.
Example:
classA.py # table definition and class A definition
classB.py # table definition and class B definition
### model.py
import classA,classB
map(classA.classA,clasSA.table)
map(classB.classB,clasSB.table)
Including mappers inside classA, and classB works, but poses cross import issues when building relations.. Maybe I am missing something :)
Take a look at Pylons project including SA setup.
meta.py includes engine and metadata objects
models package includes declerative classes (no mapper needed). Inside that package, structure your classes by relavance into modules.
Maybe a good example would be reddit source code:)
There are two features in SQLAlchemy design to avoid cross imports when defining relations:
backref argument of relation() allows you to define backward relation.
Using strings (model class and their fields names). Unfortunately this works for declarative only, which is not your case.
See this chapter in tutorial for more information.