Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
victorqribeiro committed Sep 10, 2019
0 parents commit 5cb39f9
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Victor Ribeiro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Split Django Models

Split the models file generated by Django *inspectdb* into several files with proper names, classes and imports.

## Usage

Generate your models:
```bash
python3 manage.py inspectdb > models.py
```

Run the script:
```bash
./split_django_models.sh models.py
```

Put them all into a folder so you can enjoy the step bellow:

The script also creates a *__init__.py* file, so you can import them all at once:
```python
from yourapp.models import *
```

Done!

## Buy me a coffee
[![Donations](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=victorqribeiro%40gmail%2ecom&lc=BR&item_name=Victor%20Ribeiro&item_number=donation&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted)
164 changes: 164 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models


class AuthGroup(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
name = models.CharField(unique=True, max_length=80)

class Meta:
managed = False
db_table = 'auth_group'


class AuthGroupPermissions(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
group = models.ForeignKey(AuthGroup, models.DO_NOTHING)
permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)

class Meta:
managed = False
db_table = 'auth_group_permissions'
unique_together = (('group', 'permission'),)


class AuthPermission(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING)
codename = models.CharField(max_length=100)
name = models.CharField(max_length=255)

class Meta:
managed = False
db_table = 'auth_permission'
unique_together = (('content_type', 'codename'),)


class AuthUser(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
is_superuser = models.BooleanField()
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=30)
email = models.CharField(max_length=254)
is_staff = models.BooleanField()
is_active = models.BooleanField()
date_joined = models.DateTimeField()
last_name = models.CharField(max_length=150)

class Meta:
managed = False
db_table = 'auth_user'


class AuthUserGroups(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
user = models.ForeignKey(AuthUser, models.DO_NOTHING)
group = models.ForeignKey(AuthGroup, models.DO_NOTHING)

class Meta:
managed = False
db_table = 'auth_user_groups'
unique_together = (('user', 'group'),)


class AuthUserUserPermissions(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
user = models.ForeignKey(AuthUser, models.DO_NOTHING)
permission = models.ForeignKey(AuthPermission, models.DO_NOTHING)

class Meta:
managed = False
db_table = 'auth_user_user_permissions'
unique_together = (('user', 'permission'),)


class DjangoAdminLog(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
action_time = models.DateTimeField()
object_id = models.TextField(blank=True, null=True)
object_repr = models.CharField(max_length=200)
change_message = models.TextField()
content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING, blank=True, null=True)
user = models.ForeignKey(AuthUser, models.DO_NOTHING)
action_flag = models.PositiveSmallIntegerField()

class Meta:
managed = False
db_table = 'django_admin_log'


class DjangoContentType(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
app_label = models.CharField(max_length=100)
model = models.CharField(max_length=100)

class Meta:
managed = False
db_table = 'django_content_type'
unique_together = (('app_label', 'model'),)


class DjangoMigrations(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
app = models.CharField(max_length=255)
name = models.CharField(max_length=255)
applied = models.DateTimeField()

class Meta:
managed = False
db_table = 'django_migrations'


class DjangoSession(models.Model):
session_key = models.CharField(primary_key=True, max_length=40)
session_data = models.TextField()
expire_date = models.DateTimeField()

class Meta:
managed = False
db_table = 'django_session'


class WeatherForecast(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
main_temp = models.CharField(max_length=10)
weather_id = models.CharField(max_length=5)
weather_main = models.CharField(max_length=50)
weather_description = models.CharField(max_length=100)
weather_icon = models.CharField(max_length=10)
name = models.CharField(max_length=10)
timestamp = models.DateTimeField()
geocoding = models.ForeignKey('WeatherGeocoding', models.DO_NOTHING)

class Meta:
managed = False
db_table = 'weather_forecast'


class WeatherGeocoding(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
street_number = models.CharField(max_length=6, blank=True, null=True)
route = models.CharField(max_length=150)
route_short = models.CharField(max_length=150)
locality = models.CharField(max_length=50, blank=True, null=True)
administrative_area_level_2 = models.CharField(max_length=50, blank=True, null=True)
administrative_area_level_1 = models.CharField(max_length=50)
administrative_area_level_1_short = models.CharField(max_length=2)
country = models.CharField(max_length=10)
country_short = models.CharField(max_length=2)
postal_code = models.CharField(max_length=10)
lat = models.CharField(max_length=25, blank=True, null=True)
lng = models.CharField(max_length=25, blank=True, null=True)
formatted_address = models.CharField(max_length=500)

class Meta:
managed = False
db_table = 'weather_geocoding'
36 changes: 36 additions & 0 deletions split_django_models.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

[[ $1 ]] || { echo "Usage: $0 filename.py"; exit; }

modelfile=$1

#clean comments
sed '1,10d' $modelfile -i

# split files by class name
csplit -b "%04d.py" -z $modelfile /^class/ {*}

# add import to class and rename files by class name
all="__all__ = ["
for i in xx*.py;
do
classname=$(head -1 $i | cut -d " " -f2 | cut -d "(" -f1 | tr -d '[:space:]');
filename=$(echo $classname | tr '[:upper:]' '[:lower:]')
sed '1i from django.db import models\n\n' $i -i;

imports=($(sed -n 's/.*ForeignKey(\(.*\))/\1/p' $i | cut -d "," -f1 | sed "s/'//g"))

for import in ${imports[@]}
do
importfile=$(echo $import | tr '[:upper:]' '[:lower:]')
sed "1a from .$importfile import $import" $i -i
done

mv $i $filename".py";
echo "from ."$filename" import "$classname >> __init__.py
all+="\"$classname\", "
done
all+="]"

# add all to __init__.py
echo -e "\n\n"$all >> __init__.py

0 comments on commit 5cb39f9

Please sign in to comment.