Programatically populate sample data for Django Image/File fields? - django

I need to populate sample data for Django Image/File fields in automated python code.
I want to initialize Django ImageFields and FileFields with some sample images and data files that are stored in my "site_media" directory of my Django project.
What would the code look like? This is not for testing, I want to autopopulate sample data into my Django website user's accounts (including this sample Imgae/File media.)
This should be done in python code without using fixtures.

If I understand you correctly you want to use fixtures, basically a JSON formatted file that holds data that will be put into the project database. They can be executed through the django-admin command like :
django-admin.py loaddata mydata.json
see http://docs.djangoproject.com/en/1.2/ref/django-admin/#loaddata-fixture-fixture

Related

How can I populate a django database using a script file?

I built a small django app and created some models. Now I would like to populate the database (the tables and rows do already exist) using a python script since I have to read the information I want to populate the database with, from multiple external files. Is there any way to do that?
EDIT:
Python 3.7
Django 3.0
You can always use any python routine to read files, process the content, create your model instances and save them the to the database by using a custom Django management command.
check this out: how to import csv data into django models

Django: Saving oauth provider data

I set up a simple django site using django-allauth.
I created some oauth providers in the database.
Everything is fine and working on my laptop now.
I would like to store the created database tables somehow.
Use case: I want to set up a new development environments on a different PC painlessly.
How to store the initial data of django_allauth, so that after checking out the app from git the command manage.py migrate is all I need to have the relevant database tables filled?
Django_allauth already save those data to the database, you will find them in a table *_SocialApp, here is the model code from django_auth source

How to install custom SQL for unmanaged model in Django

A simple question, is there a way Django can install custom sql for model which is marked as unmanaged ?
I have defined the custom sql file and placed it in the required directory as django documentation stands:
Django provides a hook for passing the database arbitrary SQL that’s executed just after the CREATE TABLE statements when you run migrate. You can use this hook to populate default records, or you could also create SQL functions, views, triggers, etc.
The hook is simple: Django just looks for a file called sql/"modelname".sql, in your app directory, where "modelname" is the model’s name in lowercase.
src: https://docs.djangoproject.com/en/1.8/howto/initial-data/#providing-initial-sql-data
The problem is that it does not work when the model is marked with "managed = False".
As far as I am concerned the django just append the CREATE TABLE statement with the content from custom sql file. Which explain the behavior as the CREATE statement is not present.
But is there any way Django can execute my custom sql ?
My unmanaged model is a model for database view, which I try to create with this custom sql. And I want Django to run this sql automatically while running "manage.py migrate".
I am not interested in putting the sql code inside the migration file as it is quite long and I want to keep in in the sql file.
I am using Django 1.8.
If you name your file correctly, it will be executed each time you run manage.py syncdb.
However, the naming of the file is a little tricky, and unclear from the documentation. For example, if you have an unmanaged model called MyModel in an app called MyApp running on MySQL, then you should have a file located in myapp/sql/mymodel.mysql.sql.
Also, if that file contains a SQL view, the view must be named with a format like myapp_mymodel.

bootsrapping in django

while using groovy with grails i used to use the bootstrap file to add some data such as the primary user of the application or other things that need to be initialised for the first time when the application is started , how do i achive the same in django?
You want fixtures.
See Providing initial data for models in the Django docs for more information.

Django JSON fixture generation

I'm new to Django coming from Rails, and I am finding the Django fixtures (most commonly JSON from what I gather), to be somewhat awkward and unweildy, at least in comparison to the rails fixtures that I am familiar with. I liked being able to embed some ruby code, e.g <%= Time.now %>, or reference other fixtures by name when relating things with foreign keys (so as to avoid having to keep track of ids).
So the question, how do you more experience Django developers build your fixtures. Do you sit down and write the JSON/XML/YAML by hand, or are there other tools available to help similar to what rails gives me? Or is it just easiest to create some data in ./manage.py shell and dump that data to a fixture, instead of writing the fixtures by hand? Or even still, do you find yourself putting a lot of data in your setUp(self) method in your test classes. Maybe writing these things out by hand is less tedious once you have a little more experience?
Django's admin site is great for quickly entering dummy data or initial data. You can then dump that to a json file (or whatever format).
http://docs.djangoproject.com/en/dev/ref/django-admin/#dumpdata-appname-appname-appname-model
django-admin.py dumpdata | pbcopy will dump all the data in json format to your clipboard.
Be careful with dumping contenttypes and auth tables as that may cause problems when loading the fixture back in to a database.
Check out django-dilla. It generates random data for your models, even images. Useful for testing without the tedium of manually entering data into admin.
Simple data dump of all data in project to a Json fixture
python manage.py dumpdata --format=json myapp > /path/to/myapp/fixtures/initial_data.json
Then in tests.py add this to include fixtures:
class ViewTests(TestCase):
# load fixtures
fixtures = ['data2.json']
def setUp(self):
# continue remainder of test code