POP Keywords: apache oracle sybase ...

oracle – truncate tables based on select query

PL/SQL allows to execute sql statements using a FOR loop, looping through results of another query. In below example we truncate all tables returned by another query: — Q1: empty all audit tables –/ BEGIN — Disable constraints — Truncate tables returned by SELECT query FOR entry IN ( SELECT table_name FROM user_tables WHERE table_name like ‘%AUDIT%’ — and rownum < 3 order by table_name ) LOOP –EXECUTE IMMEDIATE ‘select * from ‘ || entry.table_name || ‘ where rownum < 10’; EXECUTE IMMEDIATE ‘truncate table ‘ || entry.table_name; END LOOP; — Enable constraints END; / Note: make sure to enclose […]

django – how to empty database [SOLVED]

To empty the database, simply run: python manage.py flush To force command line w/o having to confirm: python manage.py flush —noinput sources http://stackoverflow.com/questions/10605940/what-is-the-django-command-to-delete-all-tables https://github.com/django/django/blob/master/django/core/management/commands/flush.py

django rest framework – ensure foreignKey field is optional [SOLVED]

I am using ModelSerializers and faced hardtime ensuring foreign key could be optional: models.py: class Tag(models.Model): label = models.CharField(max_length=100) group = models.ForeignKey(TagGroup, null=True, blank=True, related_name=’tags’ class TagGroup(models.Model): label = models.CharField(max_length=100) serializers.py: class TagSerializer(serializers.ModelSerializer): class Meta: model = Tag fields = (‘id’, ‘label’, ‘group’) group = TagGroupSerializer() class TagGroupSerializer(serializers.ModelSerializer): class Meta: model = TagGroup fields = (‘id’, ‘label’,) With above setup, though foreignKey field is clearly defined as nullable and not-mandatory in models.py, i kept getting following error: ‘{“group”:[“This field may not be null.”]}’ Reason for this is that you also need to specify field as nullable and non-mandatory in the […]

Configure apache to run a django framework based project [solved]

To serve django via apache, using mod_wsgi (recommended option by django documentation), proceed as follow: install apache mod_wsgi: sudo apt-get install libapache2-mod-wsgi-py3 ensure mod_wsgi is indeed into /etc/apache2/mods-enabled: ls -l  /etc/apache2/mods-enabled/ | grep “wsgi” create a python virtualenv for your project (see Run python virtualenv with Ubuntu) go to /etc/apache2/sites-available and create file myproject.conf: sudo vi /etc/apache2/sites-available/myproject.conf Paste following content: <VirtualHost *:80> ServerName mydomain.com ServerAdmin webmaster@localhost DocumentRoot /path/to/myproject ErrorLog ${APACHE_LOG_DIR}/myproject.error.log CustomLog ${APACHE_LOG_DIR}/myproject.access.log combined Alias /static/ /path/to/myproject/static/ <Directory /path/to/myproject/static> Require all granted </Directory> <Directory /path/to/myproject/myproject> <Files wsgi.py> Require all granted </Files> </Directory> WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py WSGIDaemonProcess myproject python-path=/path/to/myproject:/path/to/myproject/virtualenv/lib/python3.4/site-packages WSGIProcessGroup myproject </VirtualHost> […]

Run python virtualenv with ubuntu

By default, my ubuntu runs python2.7 (i.e. simply run python –version). This comes from /usr/bin/python –> /usr/bin/python2.7 I changed the link to point to python3 instead. BIG MISTAKE Python is used all over the place by the system => apt-get update kept failing so I had to revert back to python2.7 link To use virtualenv with python3, things are pretty easy, you simply need to prefix your commands: sudo apt-get install python3-pip sudo pip3 install virtualenv To create a virtualenv: Go to folder where you want to create the virtual env: cd /path/to/my/project To create virtual env, run: virtualenv-3-4 myvirtualenv […]

install lxml inside python virtualenv [solved]

When trying to install lxml inside virtualenv, I kept getting some compilation error messages. I already had lxml installed outside virtualenv, i.e. in my global python library. I got it installed via running: sudo apt-get install python-lxml This is of no use when using virtualenv since by definition each virtual environment will have it’s own libraries. As mentioned, pip failed when running: pip install lxml Error message: In file included from src/lxml/lxml.etree.c:239:0: /tmp/pip-build-gc7tlt4f/lxml/src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: No such file or directory #include “libxml/xmlversion.h” ^ compilation terminated. Reason it is occurring is because when installing lxml python library, a compilation occurs. […]

[django] add new field on serializer which is not defined on underlying model instance [SOLVED]

To add a field which will be computed on Serializer itself without flowing through underlying model instance, declare it as follows: class DbconfigExtendedSerializer(serializers.HyperlinkedModelSerializer): password_length = serializers.SerializerMethodField(‘getPasswordLength’) def getPasswordLength(self, instance): return len(instance.password) Simply declare your field of type serializers.SerializerMethodField(‘<method_name>’) and corresponding method will be called. sources http://stackoverflow.com/questions/18396547/django-rest-framework-adding-additional-field-to-modelserializer

[django] Could not resolve URL for hyperlinked relationship using view name “-detail” [SOLVED]

When trying to build my first rest-django app, i faced below error message, which was not easy to solve: Could not resolve URL for hyperlinked relationship using view name: “<model>-detail”. You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field. For simplicity reason, below example is based on django-rest-framework tutorial, where project is named “tutorial” and one app called “snippets” is created inside this project After investigation, the following happens: in snippets/urls.py, ensure to use “name=” attribute as per below, and use the convention of ‘<model>-detail’ for detail view […]

how to setup cx_Oracle on python [solved]

When trying to use cx_Oracle in python on windows, I faced following error: >>> import cx_Oracle Traceback (most recent call last): File “<stdin>”, line 1, in <module> ImportError: DLL load failed: The specified module could not be found. In my case, oracle instant client was not installed. Here is how to solve it. Steps Download and unzip oracle instant client appropriate for your configuration: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html note: on my side i downloaded basic client + odbc + jdbc + sqlplus Ensure environment variables ORACLE_HOME and PATH points to instant client folder note: in windows, go to control panel > System > […]

oracle – lead/lag functions returning inconsistent results [solved]

When using oracle lead() and lag() functions, I faced “inconsistent” results where running same query twice was returning different values for lead()/lag() calls. select reference_id, field1, lead(field1 1, 0) over (partition by col1, col2 order by STATUS_TIME_LONG)  next_val; This simply came from fact that my sorting key was not unique! Though STATUS_TIME_LONG recoreded entry timestamp in milliseconds, several entries within my grouping still shared same timestamp value for this field. Solution to get consistent output for lead/lag functions is simply to ensure unique-id field is part of sorting key. In my case, field reference_id is unique auto-incrementing field, so  perfect for […]