Common field types in django models

Posted In: . By CreativeSolutions


OneToOneField
    - models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)(SET_NULL, SET_DEFAULT)
ManyToManyField
    - models.ManyToManyField(Group, related_name='profiles', blank=True, null=True)
    - models.ForeignKey(User, related_name='+', on_delete=models.CASCADE) - no backward relation on setting related_name to +
CharField
    - CharField(max_length=100, blank=True, null=True, choices=CARRIER_CHOICES, default=CARRIER_CHOICES[-1][0])
    -
EmailField
    - models.EmailField(max_length=255, blank=True, null=True)
ImageField
    - models.ImageField(blank=True, null=True, upload_to=settings.UPLOAD_ROOT)
DateField
    - models.DateField(blank=True, null=True)
    - models.DateTimeField(auto_now_add=True)  - for saving new date
    - models.DateTimeField(auto_now=True) - for last modified time
    - models.TimeField()
URLField
    - models.URLField(max_length=255, blank=True, null=True)
BooleanField   
    - models.BooleanField(default=True)
IntegerField   
    - models.IntegerField(default=0)   

 

Set not null validation in django form

Posted In: . By CreativeSolutions





To set the not null validation, we need to set the 'Blank=False' in the corresponding field declaration in model
 
  Ex:
      name = models.CharField(max_length=255, blank=False, null=True)
     
      In this null=True refers to the database property

 


Instructions:

    Step 1: FreeTDS
        Download FreeTDS
        Unpack the tarball
        $]cd freetds-VERSION (where version is the FreeTDS version number)
        $]./configure --prefix=/usr/local/freetds --enable-msdblib
        $]make
        $]su root
        $]make install
        $]make clean
    Step 2: SQL Server PHP extension
        Download the source for the right version of PHP (http://php.net/downloads)
        Unpack the tarball
        $]cd php-VERSION/ext (where VERSION is the PHP version number)
        Copy the mssql/ directory to someplace where you can compile it from
        CD to that directory
        $]phpize (this requires the php-devel package)
        $]./configure --with-mssql=/usr/local/freetds
        $]make
        $]su root
        $]make install
        $]make clean
       
        If php is already installed and mssql not added follow run the below command
        $yum install php-mssql
       
       
    Step 3: FreeTDS configuration
        $]vi /etc/freetds.conf (any editor available can used for this)
        Go to the end of the file
        Add the following lines
        ;--- Custom MSSQL server name ---
        ; THIS CAN BE ANY NAMED SERVER REFERENCE YOU WANT TO CREATE
        [SERVERNAME]
        ; This is the host name of the MSSQL server
        host=HOSTNAME
        ; This is the port number to use
        port=PORTNUMBER
        ; This is the TDS version to use for anything over Server 2000
        tds version=8.0
        Save the file and exit
        $]vi /etc/ld.so.conf (Again, any editor should be able to do this)
        Go to the end of the file
        Add the following line:
        /usr/local/freetds/lib
        Save the file and exit
    Step 4: Enabling MSSQL PHP extension
        $]cd /etc/php.d
        If there is no file named mssql.ini create one
        Edit the file and enter the following lines if not already there
        ; Enable mssql extension module
        extension=mssql.so
        Save the file and exit
    Step 5: Apache (This should be done any time PHP changes)
        $]/etc/rc.d/init.d/httpd stop
        $]/etc/rc.d/init.d/httpd start
        NOTE: $]apachectl -k graceful might work as well, though httpd start offers a status flag
    Step 6: Test a file at the CLI and at the Web Server


I used the below code and got the result "Version: Microsoft SQL Server 2005 - 9.00.5000.00 (Intel X86) Dec 10 2010 10:56:29 Copyright (c) 1988-2005 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)"
< ?php
$server = 'wic018q.server-sql.com:4656';

// Connect to MSSQL
$link = mssql_connect($server, 'vs328460_1_dbo', 'Uq9y6hz3QX');

if (!$link) {
die('Something went wrong while connecting to MSSQL'.mssql_get_last_message());
}

$version = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($version);

echo "
Version: ".$row[0];

// Clean up
mssql_free_result($version);
? >

 Please visist the below url for more details
  http://www.robert-gonzalez.com/2008/03/31/connecting-php-on-linux-to-mssql-on-windows/

 

How to debug sql query in django

Posted In: . By Webdevelopmentlogics


  We can debug the django query using the 'print' command and look the terminal wher you alrady run the command 'python manange.py runserver'
  Ex:
       destination = Destination.objects.get(id = destId)
     print destination

 


To set the static folder path we need to edit the STATICFILES_DIRS variable in settings.py. Better to set the BASE_PATH+'/static' as the static folder.
  To set the BASE_PATH use the following code
   import os
       BASE_PATH = os.path.dirname(os.path.abspath(__file__))