Συχνές Ερωτήσεις: Βάσεις δεδομένων και Μοντέλα

Πως μπορώ να δω το SQL ερώτημα που κάνει το Django στο παρασκήνιο;

Make sure your Django DEBUG setting is set to True. Then do this:

>>> from django.db import connection
>>> connection.queries
[{'sql': 'SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls',
'time': '0.002'}]

connection.queries is only available if DEBUG is True. It’s a list of dictionaries in order of query execution. Each dictionary has the following:

  • sql - The raw SQL statement
  • time - How long the statement took to execute, in seconds.

Το connection.queries περιλαμβάνει όλες τις SQL εντολές – INSERTs, UPDATES, SELECTs, κλπ. Κάθε φορά που η εφαρμογή σας (ο κώδικας, αν θέλετε) αλληλεπιδρά με τη βάση δεδομένων η εντολή θα καταγράφεται.

If you are using multiple databases, you can use the same interface on each member of the connections dictionary:

>>> from django.db import connections
>>> connections["my_db_alias"].queries

If you need to clear the query list manually at any point in your functions, call reset_queries(), like this:

from django.db import reset_queries

reset_queries()

Can I use Django with a preexisting database?

Βεβαίως. Δείτε στο άρθρο Χρησιμοποιώντας μια υπάρχουσα βάση δεδομένων.

Αν κάνω αλλαγές σε κάποιο μοντέλο, πως ενημερώνω τη βάση δεδομένων;

Ρίξτε μια ματιά στην υποστήριξη του Django για τα schema migrations.

Αν δεν ενδιαφέρεστε να διαγραφούν τα δεδομένα, το manage.py έχει την επιλογή flush η οποία επαναφέρει τη βάση δεδομένων στη κατάσταση που ήταν αμέσως μετά την εκτέλεση του migrate.

Υποστηρίζουν τα Django μοντέλα primary keys πολλαπλών-στηλών;

Όχι. Υποστηρίζονται μόνο τα primary keys που ανήκουν σε μια στήλη.

But this isn’t an issue in practice, because there’s nothing stopping you from adding other constraints (using the unique_together model option or creating the constraint directly in your database), and enforcing the uniqueness at that level. Single-column primary keys are needed for things such as the admin interface to work; e.g., you need a single value to specify an object to edit or delete.

Υποστηρίζονται οι βάσεις δεδομένων NoSQL στο Django;

NoSQL databases are not officially supported by Django itself. There are, however, a number of side projects and forks which allow NoSQL functionality in Django.

You can take a look on the wiki page which discusses some projects.

Πως προσθέτω δικές μου επιλογές στις εντολές της βάσης δεδομένων CREATE TABLE, όπως προσδιορίζοντας το MyISAM ως τύπο πίνακα;

Προσπαθούμε να αποφύγουμε την εισαγωγή ειδικών περιπτώσεων στο κώδικα του Django όπως είναι ο τύπος του πίνακα κλπ, οι οποίες αφορούν επιλογές συγκεκριμένων βάσεων δεδομένων. Αν θέλετε να χρησιμοποιήσετε τέτοιου είδους επιλογές, δημιουργήστε ένα migration με τη λειτουργία RunSQL η οποία θα περιέχει τις δηλώσεις του τύπου ALTER TABLE οι οποίες θα κάνουν αυτό που θέλετε.

For example, if you’re using MySQL and want your tables to use the MyISAM table type, use the following SQL:

ALTER TABLE myapp_mytable ENGINE=MyISAM;