The Pony documentation suggests to use mixins in order to add methods to the classes automatically generated by the online editor.
The documentation shows the manually created mixins.py containing the custom methods and the automatically generated models.py containing the line class Product(db.Entity, ProductMixin):.
How do I tell the editor to automatically add , ProductMixin?
Adding it manually would defeat the purpose, because it would require to manually modify models.py in order to avoid manually modifying models.py.
Am I missing something?
You are right, this feature is not supported by the editor yet.
At this point we are preparing the PonyORM release which will include the migration tool. Once we release it, the next thing we'll do is add the feature you're talking about to the editor.
You'll be able to download a .zip file from the editor, which includes the Pony project files, such as models.py, mixins placeholder, database configuration file.
Thank you for your question.
Related
When I open a new Python file, this small dialog gives the option to make a unit test template as is in the picture. I want to change this shortcut to instead create a pytest one.
How can i do that?
And, can I put more "templates/shortcuts" in this dialog? That will save time on some templates that I use a lot to start new projects.
I already installed pytest, and went through this:
Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework.
In the Default test runner field select pytest.
But even with pytest as default, the dialog still shows "unit test" only and not pytest.
As far as I know, the selection menu for the Python file type that you get if you select New...|Python file is not configurable. You can, however, add a file template yourself, that will appear in the list of file types you get if you select New....
The easiest way to access this is via New...|Edit File Templates..., or you can get to the same dialog via Settings...|Editor|File and Code Templates. You can add templates for all new projects (schema Default), or for the current project only (schema Project), though that makes sense only for large projects.
Note that your custom file templates are always ordered alphabetically in the list, so if you want to show them in a specific order, you may use some prefix in the template names to enforce the wanted order.
Note:
I checked in PyCharm code where the internal file templates (e.g. the templates in the "New Python file" dialog) are handled, and this seems indeed to be hardcoded. Short of adapting the PyCharm code there seems to be no possibility to configure this.
I have a small program with a command line interface that uses a number of python classes with thorough implementations. I want to scrap the command line interface and wrap the app within a Django app, but I'm just learning Django and I'm unfamiliar with the conventions.
I have a number of classes, in-memory storage structures, getters/setters etc and I'd like to convert them into Django models so that I can persist them to the database and interact with them around the django app. Is there a general approach for doing something like this?
Should I just inherit the django.db.models.Model class in my existing classes and set them up for direct interaction? Or is there a better, more general/conventional way to do this?
I would like to be able to use all of this code in other apps, not necesarilly Django ones, so I don't really want to modify my existing classes in a way that would make them only work with Django. I thought of creating the models separately and then a sort of middle-man class to manage interaction of the actual in-memory class with the django model class, but that just seems like more places I have to make changes when I extend/modify the code.
Thanks for any help ahead of time...
Personally, I would modify your existing classes to extend models.Model and maintain separate versions of these classes for use outside of Django.
This will keep your classes lean and maintainable within their respective environments.
You could also create a new class that extends both models.Model and your python model through multiple inheritance. However this will result in duplicate fields for the same data.
If you would like, post an example Model as a new question and tag me in a link to it here, and I can help you convert it.
One of greatest django strengths is its ORM, if you want import i recommend you use it, and yes you would probably need rewrite the part that interacts with the database, but if you already have isolated this functions in a Models folder~classes, the modification won't be really hard
Although in your case i would recommending checking out Tornado/Aiohttp Since looks like you are just trying to create a interface for your functions
The scheme is the following. There exists a package called foo (an API under heavy development, in first alpha phases) whose rst files are auto generated with sphinx-apidoc.
For the sake of having a better documentation for foo after those files are generated, there is some editing. In, say, foo.bar.rst there are some paragraphs added to the contents generated with sphinx-apidoc
How can I not loose all that information when a new call of sphinx-apidoc is made? And of course I want potential changes in the API to be reflected, with that manual information added being preserved.
sphinx-apidoc only needs to be re-run when the module structure of your project changes. If adding, removing, and renaming modules is an uncommon occurrence for you, it may be easiest to just place the rst files under version control and update them by hand. Adding or removing a module only requires changing a few lines of rst, so you don't even need to use sphinx-apidoc once you've run it once.
I want to use django-achievements (link) module in my app, but it lack some fields in it's model. For example, I want to add CharField to it with path to picture of the badge/achievement. Also I will need to modify module's engine.py file for that.
What is the right way to do that? Download that module to my main app' folder and modify original files, or i can somehow redefine some methods/classes of original models.py and engine.py locally without modifing original files?
I'd say fork it and make your own modifications directly to the source. If it's an improvement you can create a Pull Request and contribute your code to the actual repository (not required, you can always just keep it for your own use).
I found a custom field model (JSONField) that I would like to integrate into my Django project.
Where do I actually put the JSONField.py file? -- Would it reside in my Django project or would I put it in something like: /django/db/models/fields/
Since I assume it can be done multiple ways, would it then impact how JSONField (or any custom field for that matter) would get imported into my models.py file as well?
It's worth remembering that Django is just Python, and so the same rules apply to Django customisations as they would for any other random Python library you might download. To use a bit of code, it has to be in a module somewhere on your Pythonpath, and then you can just to from foo import x.
I sometimes have a lib directory within my Django project structure, and put into it all the various things I might need to import. In this case I might put the JSONField code into a module called fields, as I might have other customised fields.
Since I know my project is already on the Pythonpath, I can just do from lib.fields import JSONField, then I can just do myfield = JSONField(options) in the model definition.
For the first question, I would rather not put it into django directory, because in case of upgrades you may end up loosing all of your changes. It is a general point: modifying an external piece of code will lead to increased maintenance costs.
Therefore, I would suggest you putting it into some place accessible from your pythonpath - it could be a module in your project, or directly inside the site-packages directory.
As about the second question, just "installing" it will not impact your existing models.
You have to explicitly use it, by either by adding it to all of your models that need it, either by defining a model that uses it, and from whom all of your models will inherit.
The best thing would be to keep Django and customizations apart. You could place the file anywhere on your pythonpath really