In django, how can I include some default entries in my models.py?
If I have a model.py like
class WidgetType(models.Model):
name = models.CharField(max_length=200)
class Widget(models.Model):
typeid = models.ForeignKey(WidgetType)
data = models.CharField(max_length=200)
How can I build a set of built-in constant values for WidgetType
when I know I will only have a few types of widgets? Clearly I could start my admin interface and add them manually, but I would like to simplify the configuration by embedding it in python.
a source to share
You can use fixtures:
http://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-data-with-fixtures
Strictly speaking, lights are not part of the models or any Python code. If you really need this in your Python code, you can listen for a signal post_syncdb
and insert your data through the ORM, for example:
from django.db.models.signals import post_syncdb
def insert_initial_data(sender, app, created_models, verbosity, **kwargs):
if WidgetType in created_models:
for name in ('widgettype1', 'widgettype2', 'widgettype3'):
WidgetType.objects.get_or_create(name=name)
post_syncdb.connect(insert_initial_data)
a source to share
You can write your own Migration
and insert new records (or do any other migration).
See this article point in the Django
docs.
a source to share