0

I have switched from sqlite to postgresql following Django: What are the best practices to migrate a project from sqlite to PostgreSQL the fourth answer.

Here are my updated settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'scraper',
        'USER':'user',
        'HOST':'localhost',
        'PORT':'5432',
    }
}

However, I cannot seem to find/grab any of the data from my database. Here's some example data from the database:

enter image description here

The migration works successfully with my postgres as I have all the tables: enter image description here

item is a table that was in the database before migration. I want to be able to access the data in item and use that to update my django model. How can I do this?

I have tried

python manage.py dumpdata

But get this:

[{"model": "auth.permission", "pk": 1, "fields": {"name": "Can add log entry",
 "content_type": 1, "codename": "add_logentry"}}, {"model": "auth.permission", "pk": 2,
 "fields": {"name": "Can change log entry", "content_type": 1, "codename":
 "change_logentry"}}, {"model": "auth.permission", "pk": 3,
... 

I wanted to upload that data based on my current app: models.py

from django.db import models

class Cruises(models.Model):
    title = models.CharField(max_length=200)
    #ship_name = models.CharField(blank=True, null=True,max_length = 200)

    def __str__(self):
        return self.title

views.py

from django.shortcuts import render
#from .models import Cruises
from .models import Cruises
def basic(request): 
    #form_destination =  Cruises
    long_list = Cruises.objects.values('title')
    return render(request, 'cruise_control/basic.html', context = {'long_list':long_list})

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('',views.basic, name = 'basic')
]

basic.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Cruises</title>
  </head>
<body>
<h1> Cruise Control </h1>


<form action="/action_page.php">
  <label for='destination'>Destination</label>
  <input type="text" list="destination" />
  <datalist id="destination">
  {% for lng_l in long_list %}
  <option>{{lng_l.title}}</option>
  {% endfor %}
  </datalist>
  <!label for="cruisetime">Departure date</label>
  <!input type="date" id="cruisetime" name="cruisetime" min={{dep_l.departureDate}}>
  <!input type="submit">
</form>

</body>
</html>

However, because it grabs none of the data I get the following without any values in the drop-down: enter image description here

tesla john
  • 310
  • 1
  • 2
  • 9

2 Answers2

1

i can not find the question, but i can say:

  1. dump all data from your_app_name

    ./manage.py dumpdata your_app_name > your_model_data_filename.json

2.dump all data from your_model_name model in your_app_name app

./manage.py dumpdata your_app_name.your_model_name > your_model_data_filename.json
  1. load data, if you are already did migration and named properly app names and model names

    ./manage.py loaddata your_filename.json

Be careful: if dump not works for utf8, try to change --format option to xml. json by default.

After update the question, i see, this is the question about work with legacy table:

Full doc here: https://docs.djangoproject.com/en/4.0/howto/legacy-databases/

In steps:

  1. create a sketch of model "Item": python manage.py inspectdb item > item_model.py

  2. Check output and add this model to app cruises.

  3. Check meta in model Item. It should be:

    class Meta: managed = False db_table = 'items'

  4. After that you can use Model Item like Model Cruises. Read attributes create new item etc.

  5. As additional you can create empty migration and manage data from Item into model Cruises accordingly. In young Django version in "South" it called "DataMigration".

Maxim Danilov
  • 2,472
  • 1
  • 3
  • 8
  • I get an empty list when I do this and nothing contained in the json. I have migrated properly also as I can see all the django and auth tables in my pgAdmin IDE, however I have an extra table named `item` that already existed in there but I want to use it in my django app. How can I do this? – tesla john Jul 04 '22 at 21:35
  • I have added an image and a concise question to what I am struggling with – tesla john Jul 04 '22 at 21:38
  • You can change table_name for every dict in your json from "item" to "cruises_cruises", or you can create a empty migration, and move information from "items" table to "cruises_cruises" with python code. – Maxim Danilov Jul 04 '22 at 21:46
  • Thank you for these suggestions, I am happy to proceed with the second as the first has only an empty list so I cannot change anything. Any suggestions on how to adopt the second option? Would it be better to read the data from postgres - "cruises_cruises" and "items", then merge on the columns or keep the columns that I am using and then send it back to postgres? However, not sure whether this updates django though as "item" did not get affected by migration. What is the best way for this? – tesla john Jul 04 '22 at 21:54
0

Please try

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'scraper',
        'USER':'user',
        'HOST':'localhost',
        'PORT':5432,
    }
}

django.db.backends.postgresql_psycopg2 used in django <1.8 and it was renamed in django 1.9 to django.db.backends.postgresql

  • postgresql_psycopg2 works im 4.1beta normally. He don't need to rename it. – Maxim Danilov Jul 04 '22 at 21:21
  • You are right. in release note: Database backends The PostgreSQL backend (django.db.backends.postgresql_psycopg2) is also available as django.db.backends.postgresql. The old name will continue to be available for backwards compatibility. https://docs.djangoproject.com/en/4.0/releases/1.9/ – Ferhat Mousavi Jul 04 '22 at 21:34