reusable app (package) in Django - how to add extra models - django

I am writing a small package that extends the django app that is used by many of my colleagues locally. So right now they can simply add it via pip, and then they add this extension in INSTALLED_APPS in settings.py.
But the problem is that I can't add the new models to this extension (or at least I don't figure out yet how to do it correctly) because then the guys who would like to use my extension, have to sync their database or make migrations so their database contains the models needed for extension.
Is it possible (and right thing to do) to add new models to the current django project 'silently' as soon as the user adds the app to INSTALLED_APPS?

Related

How to store third party apps migrations in django

I'm fairly new to python and django, and trying to build a simple calendar based on django-scheduler package.
According to django-scheduler docs, a custom base class can be used to add additional fields, managers and such.
So, I used an abstract model to add a new field:
#myproject/customer_calendar/models.py
from django.db import models
from main.models import Customer
class CalendarAbstract(models.Model):
customer = models.OneToOneField(to=Customer, null=True, blank=True, related_name='calendar')
class Meta:
abstract = True
And added this to settings.py
SCHEDULER_BASE_CLASSES = {
'Calendar': ['customer_calendar.models.CalendarAbstract'],
}
Now, if I use makemigrations command, a new migration is created inside scheduler app (which is located in site-packages of the current virtual env), which doesn't allow me to keep track of migrations via VCS.
I've found a couple of solutions:
1) Keep the whole scheduler app inside my project. According to SO it' s considered a bad practice and third-party apps should always be retrieved via pip.
2) Use django setting to store all django-scheduler migrations inside my calendar app
MIGRATION_MODULES = {
'schedule': 'customer_calendar.migrations',
}
The second one looks good to me, but I don't know if it's considered to be a valid solution to this problem.
Is there any other ways to store third-party apps migrations?
The second one looks good to me, but I don't know if it's considered
to be a valid solution to this problem. Is there any other ways to
store third-party apps migrations?
As also stated in this answer, FeinCMS docs recommend the use of MIGRATION_MODULES to monitor the migrations of FeinCMS as a third-party app.
FeinCMS itself does not come with any migrations. It is recommended
that you add migrations for FeinCMS models yourself inside your
project.
...
Create a new folder named migrate in your app with an empty init.py inside.
Add the following configuration to your settings.py:
MIGRATION_MODULES = {
'page': 'yourapp.migrate.page',
'medialibrary': 'yourapp.migrate.medialibrary', }
You must not use migrations as folder name for the FeinCMS migrations,
otherwise Django will get confused.

Migrate models outside current app

I'm using django-modeltranslation in a Django 1.8 project. This app generates fields on the fly to store translations. It's possible to mark models as translatable by creating an Options class, like registering models with the Django admin.
I marked a model from a third party app as translatable. Django's migrations system picks up the changes and generates migrations in the app outside of my project. I want to store these migrations in an app in my project, how can I do that? I can only give a model name to migration operations, not a fully qualified app name.model name string.

CMS for Django when upgrading app from 1.5.5 to 1.7.1

I have a huge... challenge in front of me. For about a week or two I've been migrating 1.5.5 django project to 1.7.1. A huge jump, many deprecated variables, methods and so on.
In 1.5.5 there were some south migrations done but not everywhere, as it was not implemented from the beginning. So let's say there are no migrations, they have to be created.
Also there is a wish to add a cms to the already upgraded project, but with django-cms-3.0.7 I constantly encounter some issues with migrations, south existing etc.
Is there a CMS that I can use with this app that won't be bothered by migrations and django version?
All I want to edit is the static content (text, images, maybe adding videos) before user logon. No integration with models. Just some info pages.
Any suggestions?
A maybe oversimplified solution for this could be django-front. Create your static pages and add the fields you want to edit. You edit it with a wysiwyg editor. I use it for my terms of service/privacy policy.
You will probably be always bothered by migrations and django version when using an app that brings extra functionality, but the apps should not be hard to upgrade and normally they have a warning/walk through when an important change on their arquitecture/functionality has happened.
That being said, i don't think migrations change dramatically now. The change to include them in the django project was an important (and needed) one.
If you want something even more simple (and time resistant) just create a model for your pages and render it on your template:
class Content(models.Model):
html_content = models.TextField()
image_content = models.ImageField()
Register that model to your admin and that should do the trick. For simple applications this may be enough.

Where is recommended spot for storing admin customizations for Django contrib apps?

I want to add Django Sessions to my Django Admin, and I am following an SO post about this, but it is unclear where I store this code. Do I put it in an admin.py file? Under what directory?
In short, it doesn't matter. You can put the code into any of your apps' admin.py files. However, in situations like these I tend to use a generic app in my project, usually named something like utils, that exists for the sole purpose of housing code that doesn't belong to one specific app or could be used by multiple apps.
If you want to be more specific, you can create a sessions app in your project specifically devoted to this code and any other code related to session management for your project, or perhaps an existing app that is somewhat related. For example, I put customizations to the User admin in my accounts app that holds the UserProfile model.

Django not creating tables for an installed app

My django site was functioning before I installed Lion and had to reinstall everything related to development. Since then, I have deleted and recreated my database, but one of my two installed apps is being ignored in syncdb. Those tables are not present in my database.
This post suggested there might be an import error. I can import the app in question using manage.py shell, so I don't think that's it.
Both apps are definitely installed (verified by debug toolbar). Any other suggestions? I'm relatively new to Django, having been mostly an iOS developer for the past couple of years.
https://docs.djangoproject.com/en/dev/ref/models/options/#app-label
If a model exists outside of the standard models.py (for instance, if the app’s models are in submodules of myapp.models), the model must define which app it is part of.
What it doesn't mention is that they also have to be imported somewhere during the model registration phase.