D
D
Dmitry Filyushin2015-01-27 10:28:27
Django
Dmitry Filyushin, 2015-01-27 10:28:27

How to make a raw request in Django?

There is this model:

class Orders(models.Model):
    id = models.AutoField(primary_key=True)
    idhospital = models.IntegerField(blank=True, null=True)
    idfinance = models.ForeignKey('RefFinance', db_column='idfinance')
    idtest = models.IntegerField(blank=True, null=True)
    iddiagprimary = models.IntegerField(blank=True, null=True)
    idbiomaterial = models.IntegerField(blank=True, null=True)
    iddiagsecondary = models.IntegerField(blank=True, null=True)
    ordertype = models.SmallIntegerField(blank=True, null=True)
    ordernum = models.CharField(max_length=100, blank=True)
    orderdate = models.DateTimeField(blank=True, null=True)
    orderprice = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)
    dategetbm = models.DateTimeField(blank=True, null=True)
    orderready = models.DateTimeField(blank=True, null=True)
    dategetorder = models.DateTimeField(blank=True, null=True)
    userready = models.CharField(max_length=30, blank=True)
    patient = models.CharField(max_length=1024, blank=True)
    iin = models.CharField(max_length=1024, blank=True)
    dob = models.DateTimeField(blank=True, null=True)
    sex = models.SmallIntegerField(blank=True, null=True)
    address = models.CharField(max_length=1024, blank=True)
    workplace = models.CharField(max_length=1024, blank=True)
    depart = models.CharField(max_length=1024, blank=True)
    sector = models.CharField(max_length=1024, blank=True)
    palata = models.CharField(max_length=1024, blank=True)
    numhistory = models.CharField(max_length=100, blank=True)
    isdelete = models.BooleanField()   
    class Meta:
        managed = False
        db_table = 'orders'

For a new record, the ordernum field must be formed according to a special principle. The principle is implemented as a function in postgresql and can be called as select getordernum(:idvalue). IdValue must be passed before the request is executed.
I would like to get something like this:
order = Orders(patient=fio)
order.idhospital  = ...
order. ... = ..
<b>order.ordernum  = Orders.GetNewNum</b>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
silentboy13, 2015-01-27
@Filyushin

well, something like this
Person.objects.raw('SELECT * FROM myapp_person')

D
Dmitry Filyushin, 2015-01-27
@Filyushin

A few minutes later, an insight came from the off-site of the django project

...
    class Meta:
        managed = False
        db_table = 'orders'
    def GetNumOrder(self, idOrder):
        from django.db import connection
        cursor = connection.cursor()
        cursor.execute("SELECT getordernum(%s) as id", [idOrder])
        row = cursor.fetchone()
        return row[0]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question