I'm new to python, and starting to learn website development with pylons and sqlalchemy.
I've read the document of sqlalchemy and pylons, but still have a lot of problems. I've tried 2 days, but a simple website with basic CRUD operations can't work yet. I met some big problems(for me), that the circular imports problem, and relationship between models. I want to ask them here, but I know little about python, it's a problem for me to ask too.
I'm looking for an example application using pylons and sqlalchemy, I've googled, but not found. Where can I found it? Thanks in advance!
You should read The Pylons Book.
You should probably start looking from here, http://wiki.pylonshq.com/display/pylonscommunity/Sites+Using+Pylons as many of them are open-source.
Another source would be PyPI: http://pypi.python.org/pypi?%3Aaction=search&term=pylons&submit=search
Good (but complex) example on Pylons + SQLA is reddit: http://code.reddit.com/browser/r2/r2/
I met some big problems(for me)
It easier to just ask about these particular problems, though, rather than trying to understand existing code. Sites like reddit use some un-intuitive code.
circular imports problem
Just use single module per class and there will be no problems. When it is absolutely nessesary that class X and class Y able to use each other, use
from .y import Y
Class X(Base):
...
y = relation(Y, backref="x")
Class Y(Base):
...
#classmethod
def get_x(cls):
return cls.x.attr.target_mapper.class_
This is a bit hackish, but lets you create circular reference. Other way would be to add X into module y namespace from module x explicitly.
Related
I'm building a project using flask and flask-SQLAlchemy (with sqlite3 and with python 3.8). I have models.py which holds all the tables of the db, and I want to have static method which deletes rows in Articles table depends on one of their attribute.
I thought about writing this funciton in the models class and wanted to ask if that's fine.
The function looks like:
def update_articles():
all_articles = Articles.query.all()
for article in all_articles:
if needs_to_be_deleted(article):
db.session.delete(article)
db.session.commit()
Is that funciton fine (don't mind the needs_to_be_deleted(article) thing). Is the way I delete the article good? and is the place for this function can be at the models.py file?
As per my experience its all good here. As a general rule you should design your application as:
Models should do the most powerful stuff
Views should carry out only logical work
Templates should be dumb. They should just render things.
Now by saying the above i mean to say all the stuff relating to the operations with database should be in the models. Just like you are doing. Most of the time all the transactions with your database are simple ones and can be written with a couple of lines with ORM. But sometimes if you feel that something needs to be done over and over again or you feel like having a method for doing something big. Then its better to have that in your models only. The method you mentioned should be a part of your Article model. Other things that can be there in your model could be operations like sending emails to users or some other routine tasks.
As far as your views are concerned. They should carry out all the logical stuff. Your business logic is basically implemented by the views.
Lastly templates should do the work of rendering things whatever is fed to them by the views.
Obviously there are no such hard and fast rule. There can be exceptions to the above. Or someone could find any other way of doing things better rather than that i mentioned. For that you need to use your own conscience and no one can teach you that. It comes with experience.
For your reference please go through the following articles:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
https://flask.palletsprojects.com/en/1.1.x/tutorial/
Also you might be using this but in case your aren't:
https://flask-marshmallow.readthedocs.io/en/latest/
Just one small suggestion too. though i am not clear about your requirements. But it would be better if you could commit after the for loop i.e at the very end of the method. A request should generally have only one commit.
So, I'm looking into the Django and Neo4j integration, but there's not much about it out there ... What I want to know is if I have a model like the one here:
If I want to add a new property to a model, it would be as simple as doing:
node.setProperty( "newProperty", "something" );
??
Moreover, all the queries in django would work ? How is the traversing made ?
I would appreciate any response
:D
Thanks.
We've been working on updating the Neo4j/Django integration to work with neo4j-rest-client - the fruits of our labor are on GitHub, with some quick comments on my blog.
There are some pros and cons to our integration. The most obviously impacting is our use of a REST client- you get to use a remote database, while losing quite a bit, performance-wise. OTOH, the integration works alongside a relational database, so you can still use django.contrib stuff that relies on the original ORM, and it handles indexing and query sets pretty nicely.
To do what you want above using neo4django, you'd simply get the neo4j-rest-client based node from the model instance, and have at it.
model_instance.node['newProperty'] = 'something'
We're still cranking on making the integration more dynamic, supporting traversals/etc in a Pythonic way, and (currently most important) improving the performance. If you're interested, I'd love feedback.
Have you seen Tobias's blog post about Django integration? It's kinda old now, but still relevant. Plus there are examples you can check out too.
So I have a function which creates a dynamic model. I accomplish this in a way very similar to AuditTrail (see django wiki).
Sample of code is here:
https://gist.github.com/0212845ae00891efe555
Is there any way I can make a dynamically-generated class pickle-able? Ideally something thats not a crazy monkeypatch/hack?
I am aware of the problem where pickle can't store a generated or dynamic class. I solved this by rigging in my dynamic type into the modules dict like so:
new_class = type(name, (models.Model,), attrs)
mod = sys.modules[new_class.__module__]
mod.__dict__[new_class.__name__] = new_class
It's FAR from a clean or elegant solution, so if someone can think of a more django-friendly way to make this happen, I am all ears. However, the above code does work.
The reason there aren't answers for this is because the answer is likely hackish. I don't think you can unpickle an object in Python without knowing the structure of the class on the receiving end without some sort of hackish solution. A big reason pickle doesn't support it is probably because it's a fantastic way to introduce malicious code into your application.
http://www.mofeel.net/871-comp-lang-python/2898.aspx explains a bit why dynamically created classes can't be unpickled.
In every case, I've either just serialized a dictionary of the attributes of the object using the dict method, or just figured out some awful work around. I hope you come up with something better.
Good Luck!
I've been getting more and more interested in using Pylons as my Python web framework and I like the idea of MVC but, coming from a background of never using 'frameworks/design patterns/ what ever it\'s called', I don't really know how to approach it.
From what I've read in the Pylons Book, so far, it seems I do the following:
Create my routes in ./config/routes.py
This is where I map URLs to controllers.
Create a controller for the URL
This is where the main body of the code lies. It does all the work and prepares it for viewing
Create my template
I create a template and assign the data from the controller to it
Models... I have no idea what they're for :/
So my question is, can you recommend any reading materials for someone who clearly has no idea what they're doing?
I really want to start using Pylons but I think in a few months time I'll come back to my code and think "...what the F was I thinking :/"
EDIT: A better, summarized, question came to mind:
What code should be placed in the Controller?
What code should I put in the Model?
The view is just the templating, right?
And, in terms of Pylons, the 'lib' folder will contain code shared among Controllers or misc code that doesn't fit anywhere else - Right?
there is a book about pylons 0.9.7 [http://pylonsbook.com/].
and after that see the updated docs to understand pylons 1 at [http://bitbucket.org/bbangert/quickwiki]
and [http://bitbucket.org/bbangert/pylons].
if you have a question go to the google groups for pylons [http://groups.google.com/group/pylons-discuss]
Model is for your db-related code. All queries go there, including adding new records/updating existing ones.
Controllers are somewhat ambigous, different projects use different approaches to it. Reddit for example does fair bit of what should be View in controllers.
I, for one, prefer to limit my controllers to request processing and generation of some result object collections, which are then delivered to XHTML/XML/JSON views, depending on the type of request (so each controller should be used for both static page generation and AJAX handling).
I really want to start using Pylons but I think in a few months time I'll come back to my code and think "...what the F was I thinking :/"
Well, thats inevitable, you should try different approaches to find the one which suits you best.
I understand that there are big differences in data-store, but surely since django is bundled and it abstracts data-store away from Satchmo, something can be done?
Truth is that I am not a Python guy, been mostly Java/PHP thus far, but I am willing to learn.
Plus, if this is not possible today, lets band together and form a new Open Source project to "extend" satchmo or perhaps branch it, for compatibility?
Possible if:
Someone writes a generic ORM to Bigtable mapper. Most probably, Appengine Patch Guys
Someone rewrites the views and models of Satchmo to remove existing ORM queries and use the minimal functionality of the ORM provided by the patch project, should be either you or the Satchmo guys.
Someone hacks around a lot using the django helper project, can only be helper project guys.
You can't. There are alot of dependencies in Satchmo that you aren't allowed to install on AppEngine.
See this thread as well: http://groups.google.com/group/satchmo-users/browse_thread/thread/509265ccd5f5fc1e?pli=1
Nothing is impossible - this will just require lots of effort - if there will be somebody wishing to do so - why not? But it might be easier (cheaper) to get Django friendly hosting instead of spending hours on hacking the code.