syncdb-like tool for Kohana - django

I've used Django(python) previously and now I'm learning Kohana (PHP). It seems to be good, but I wonder if there is something like "django-admin.py syncdb" in Kohana, or at least, a tool that makes the same job.

Django Model knows all information about their field before start working with database. Kohana Models, in contrast, knows only table name and on first using ORM authomaticaly calls ORM::list_columns() which executing sql query
SHOW FULL COLUMNS FROM `tablename`
and fill protected $_table_columns variable for this Model

Related

GeoDjango + Postgis missing functions

So I've been using a Postgres database with the extension Postgis installed, and then using a Django setup to draw spatial data on a Leaflet OpenStreetMap. It has been a bit difficult translating my sql queries to the database functionality that Django is using whenever you're communicating with your database. Often I seem to be missing Postgis functions such as ST_LineCrossingDirection or ST_FrechetDistance.
How do I get to use those functions along with .annotate and .filter from Django without having to write custom sql queries and executing those?
I've tried to look into F() and Func() from Django as well, but I don't think that solves my issue as it seem to be using built in aggregate functions. I also tried to execute RawSQL in an annotate function to perform the function ST_LineCrossingDirection but it would require me to write a WHERE clause and the condition is something I'm not aware of until I get to the filter() call where I'm using intersects() between two geometries.
Anyway .. If anyone knows how to use what I assume is missing Postgis functions please let me know. Because my code is getting quite messy and ineffective.
Thanks, and all help is appreciated!
You should take a look at GeoFunc, it's the GeoDjango equivalent of Django's Func.
from django.contrib.gis.db.models.functions import GeoFunc
class LineCrossingDirection(GeoFunc):
function='ST_LineCrossingDirection'
class FrechetDistance(GeoFunc):
function='ST_FrechetDistance'

Django test -- Model object created but cannot be found

I'm calling a Model which is called People and do
People.objects.create(first='foo', last='bar', bio='test')
This Model uses db_table='"people"."background"'
When I run the test, doing People.objects.first() finds something, but doing raw query like SELECT * from people.background gives me nothing. Why is that?
Apparently Django doesn't officially support schema.
I have come up with a workaround which connects to the db and makes a raw query directly. Essentially,
with connection().cursor as cursor:
cursor.execute("""INSERT INTO bleh bleh bleh""") # assuming there's autocommit
EDIT:
Django's response: Django doesn't officially support schemas. See #6148 for that. As far as I know, the . syntax only works on Oracle.

Create function in database using django ORM

In my project I want to get people who have birthday between some days, I hope to find a solution which does not force any limitations to queries.
I have found this solution which seems efficient and suite for my problem. But now I have a second problem to create the function in database using django ORM, because this must be portable and works with test database also. I could not find any proper way to able to define the function and the index based on it in django.
In brief I want to create below function in database using django:
CREATE OR REPLACE FUNCTION indexable_month_day(date) RETURNS TEXT as $BODY$
SELECT to_char($1, 'MM-DD');
$BODY$ language 'sql' IMMUTABLE STRICT;
CREATE INDEX person_birthday_idx ON people (indexable_month_day(dob));
To answer your question, using RunSQL you can insert raw SQL into a migration
-- it looks like you should be able to put this raw SQL into a migration file, including the function that would create the custom index. So running the migration would create the custom in
But don't do this -- you should just use Django to index the dob field, i.e.
dob = models.DateField(db_index=True)
and use Django to write your queries as well.

Django: Possible to load fixtures with date fields based on the current date?

I want to load fixtures into django. The data has some date fields - is it possible to create these data so they will always be e.g. yesterday or tomorrow? I want to make sure certain data is always fresh, but also so I can easily test edge cases (e.g. whether an object is enabled if the publishing date is today, etc).
Fixtures just load text data files (in JSON/XML/YAML) so, there's no real way to insert dynamically generated data by just loading a fixture. On the other hand, you can get around this using other methods.
One option is the package django-fixture-generator where you can write python/django code to create data and it will be inserted before your tests are called.
Another option is a previous SO question: How to load sql fixture in Django for User model?. This has some code on using SQL files for fixtures, where you can use a SQL expression for your date requirements (e.g. GETDATE()+1 or similar in your SQL dialect).

Proper way to call a database function from Django?

i'm triyng to make a full text search with postgresql and django So I've created a function search_client(text) which returns a list of clients. To call it from the DB i use something like this:
SELECT * FROM search_client('something')
and i'm not really sure how to call it from django. i know i could do something like
cursor = connection.cursor()
cursor.execute("SELECT * FROM search_client('something')")
result = cursor.fetchall()
but that will only return a list of values, and i'd like to have a list of objects, like when i use the "filter()" method.
Any ideas?? thanks for your time!
If your goal is a full-featured search engine, have a look at django-haystack. It rocks.
As for your question, the new (Django 1.2) raw method might work:
qs = MyModel.objects.raw("SELECT * FROM search_client('something')")
If you're using Django 1.2, you can use the raw() ORM method to execute custom SQL but get back Django models. If you're not, you can still execute the SQL via the extra() method on default QuerySet, and pump it into a custom method to either then go pull the real ORM records, or make new, temporary, objects
First, you probably don't want to do this. Do you have proof that your database function is actually faster?
Implement this in Python first. When you can prove that your Python implementation really is the slowest part of your transaction, then you can try a stored procedure.
Second, you have the extra method available in Django.
http://docs.djangoproject.com/en/1.2/ref/models/querysets/#django.db.models.QuerySet.extra
Note that compute-intensive database procedures are often slow.