How to install PostGIS for Django? - django

So I'm following the DOCS and just want to make sure I'm understanding correctly.
https://docs.djangoproject.com/en/1.10/ref/contrib/gis/install/postgis/
Do I just create a file called migrations.py with:
from django.contrib.postgres.operations import CreateExtension
from django.db import migrations
class Migration(migrations.Migration):
operations = [
CreateExtension('postgis'),
...
]
and drop it in my project directory? And then run python manage.py makemigrations ?

Still the better way ist to create extension directly by making a sql query:
CREATE EXTENSION postgis;
After that you just have to navigate to your project-root (there is a manage.py file inside) and run python manage.py migrate (since django 1.9 - before v.1.9 first run python manage.py makemigrations and after that python manage.py migrate)
But if you want to use your code, you have to add it to "models.py".
This is the file called by "python manage.py migrate"
So your models.py looks like:
from django.contrib.gis.db import models
from django.contrib.postgres.operations import CreateExtension
from django.db import migrations
class Migration(migrations.Migration):
operations = [
CreateExtension('postgis'),
]
class model1(models.Model):
geom = models.GeometryField(srid=4326,blank=True,null=True)
name = models.TextField(null=True)

Related

Django deploy errors

I just launched the Django app. As an image, everything is in place, but the form and admin panel do not work. Anyone who knows please help
I get this error when I run the form.
Let me know if I need to share any code.
model.py
from django.db import models
# Create your models here.
class Form(models.Model):
fname_lname = models.CharField(max_length=50, verbose_name="Ad Soyad")
number = models.CharField(max_length=20, verbose_name="Nömrə")
send_date = models.DateTimeField(auto_now_add=True, verbose_name="Tarix")
class Meta:
verbose_name = 'Formlar'
verbose_name_plural = 'Formlar'
def __str__(self):
return self.fname_lname
As Marco suggested, once you deploy you should run your migrations since you're probably using a different database. You run migrations same as when in development depending on your platform the following should work:
python manage.py makemigrations
python manage.py migrate
just make sure you are on the same directory as the manage.py file. Also remember that you will have to tweak the settings.py file database settings if you haven't already.
try the commands below:
python manage.py makemigrations --fake
python manage.py migrate --fake
if it donot work, i think you have to delete database and recreate it,
if you need the data you can use python manage.py dumbdata ->data.json
and after you create the new database use python manage.py loaddata data.json

python3 manage.py makemigrations No changes detected

(fcdjango_venv) Subinui-MacBook-Pro:Impassion_community subin$ python3 manage.py makemigrations
No changes detected
I'm learning Basic Django right now, and was following the lecture, but got problem.
I followed the lecture, so first I typed the code on models.py
from django.db import models
# Create your models here.
class Impassionuser(models.Model):
username=models.CharField(max_length=64,
verbose_name='사용자명')
password = models.CharField(max_length=64,
verbose_name='비밀번호')
registered_dttm = models.DataTimeField(auto_now_add=True,
verbose_name='등록시간')
class Meta:
db_table = 'Impassion_Impassionuser'
and then on Terminal, I typed
(fcdjango_venv) Subinui-MacBook-Pro:Impassion_community subin$ python3 manage.py makemigrations
but the result was
No changes detected
In the lecture, after typing python3 manage.py makemigrations
it shows
Migrations for "User" :
user/migrations/0001_initial.py
- Create model User
how can I get the same result?
Include the name of your app after the makemigrations command and make sure you have a migrations folder created within your app.

Django won't import values to database - sqlite3

I am trying to put values to my django database.
My app_name/models.py looks like:
from django.db import models
# Create your models here.
class Language(models.Model):
id = models.AutoField(primary_key=True)
code = models.TextField(max_length=14)
I ran this commands:
python3 manage.py makemigrations app_name
python3 manage.py migrate app_name
After that I starting importing values to my database. So I run this command:
python3 manage.py shell
and put this code to the shell:
from app_name.models import *
with open('lang.txt', 'r') as file:
for i in file:
a = Language.objects.create(code=i.strip())
File lang.txt contains 309 lines with language codes. The file looks like:
en
fr
af
de
...
When I run this code in the manage.py shell Django created 309 Language objects. But when I try type random id in the shell -> Language(id=1).code it return only empty string - ''.
I tried use save() method too but still same problem.
from definitions.models import *
with open('lang.txt', 'r') as file:
for i in file:
a = Language(code=i.strip())
a.save()
Django version 1.10.1
So my question is where can be a problem?
This is happening because you are not fetching Language objects properly.
When you do Language(id=1).code, You are instantiating Language object with no value in given to code field.
In django, to fetch a object, you do Model.objects.get(field=value). So your code becomes:
Language.objects.get(id=1).code

Migration issues in python django?

I have MySQL database in backed but when i change any model i does not reflect in backend
python manage.py makemigration app_name
python manage.py migrate
but it show no migrations applied
does mysql does nor support migrations
my model
class blog(models.Model):
name = models.CharField(max_length=10)
try fake migration
python manage.py -fake app_name
Be sure that you already added app_name to INSTALLED_APPS list

Add a field to Mezzanine blogpost

I am using Mezzanine for a project. I need to add a extra field to Mezzanine blogpost.
I notice using EXTRA_MODEL_FIELDS can do it, but it looks complex.
I also try copy the blog folder from the site-package to my project path, and then modify the models.py. but I doesn't work.
I am new to Django, can some one help?
Thanks
By do some research, now I got the answer:
1. copy the blog app from sites-package to my project
2. change my setting.py
INSTALLED_APPS = (
"blog", #it was "mezzanine.blog",
.....
3. modify the blog/models.py
add following line to class BlogPost
shop_url= models.CharField(max_length=250,null=True, blank=True)
4. migirate the table (installed South)
./manage.py schemamigration blog --auto
./manage.py migrate blog
You can create a django app (CustomBlog), add it to your installed apps
and remove or comment the Mezzanine blog:
INSTALLED_APPS = (
"CustomBlog", #it was "mezzanine.blog",
...
)
In the models.py and admin.py, of your CustomBlog, inherit from the class BlogPost of Mezzanine:
models.py
from django.db import models
from mezzanine.blog.models import BlogPost
from mezzanine.blog.models import BlogCategory
class CustomBlog(BlogPost):
# Add New Field
# example
new_field = models.CharField(max_length=255)
class CustomBlogCategory(BlogCategory):
pass
admin.py
from django.contrib import admin
from .models import CustomBlog,CustomBlogCategory
admin.site.register(CustomBlog)
admin.site.register(CustomBlogCategory)
Then in the terminal create and run migrations
python manage.py makemigrations
python manage.py migrate