Re: Django 1.6.11 model class query

2015-11-24 Thread Nikunj Badjatya
Better late than never. Thank you !
This really helped.


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e2363245-935b-4cd7-a7c7-5adc5c051c19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django 1.6.11 model class query

2015-08-19 Thread 'Tom Evans' via Django users
On Wed, Aug 19, 2015 at 3:18 PM, Nikunj Badjatya
 wrote:
> Hello All,
>
> I have a django model class defined as:
>
>
> class Test(models.Model):
>id=AutoField(primary_key=True)
>name = models.CharField(max_length=45, null=False)
>description = models.CharField(max_length=200, null=True)
>uuid = models.CharField(max_length=250)
>class Meta:
>managed = False
> db_table ="test"
>
> Where,
> uuid is auto generated using a mysql trigger.
>
> Mysql Trigger looks like:
> DROP TRIGGER IF EXISTS `test_uuid`//
> CREATE TRIGGER `test_uuid` BEFORE INSERT ON` test
> `
> FOR EACH ROW begin if (new.uuid is  null or new.uuid = '') then set new.uuid
> = (select uuid());end if; end
> //
> This trigger is added at the db level.
>
> i.e., any new insert in 'test' table would generate the value for uuid
> column.
> and insert works fine for this model from the code.

Why do you want to do this way?

>
> Issue is, when I try to access the uuid after I have performed the save(), I
> am getting null.
>
> row = Test(name=in_data['name'],
>   description=in_data['description'],
>)
> row.save()  # This creates a row with id, name, description and uuid (auto
> generated by the pre insert rigger)
> print(row.id)   # Prints id
> print(row.uuid) # Prints u' '
>
>
>
>
> Any idea as to what may be going on?
> Why row.uuid is coming as empty from the code though the uuid is clearly
> generated in the db table.
>

When you save something to the database, Django does not re-select out
the data that you just saved, and the contents of a model instance are
exactly as they were before you saved it - as far as django is
concerned, it has just persisted the most up to date version of it
*from* that model instance, so it would be wasteful to re-select it
out.

This is because django is an ORM, it knows about its own data
serialization, but nothing about your DB level trigger modifying the
data that it serializes.

You have two straightforward options - move the uuid setting code to a
django pre-save hook (ideally using the built in UUID field), or every
time you save an object, remember to re-fetch it from the database.

Whatever you do, presumably you are using UUIDs as an opaque
identifier, and will query the database by UUID to find an object, and
so the UUID field should be declared as db_index=True.

Cheers

Tom

PS:

UUIDs are 128bit integers - 16 bytes. Storing a 128 bit integer as a
variable length string of up to 250 bytes is going to be massively
inefficient, so you really should use a UUID type (new in 1.8) so that
it uses the best type it can for your database backend, either native
uuid or a fixed 32 char string.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1LyK4E9BqF6%2B3ZXAqJz64f8sExB6fvgAz6OUtbuiqPZ9A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django 1.6.11 model class query

2015-08-19 Thread Vijay Khemlani
I'm guessing Django does not update all of the fields of the instance when
saving it, only the id

On Wed, Aug 19, 2015 at 11:31 AM, Nikunj Badjatya 
wrote:

> Also,
> When the same object is retrieved using
>
>
> print(Test.objects.get(id=row.id).uuid)  # Prints uuid.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/75749d6e-f52e-4e36-97ec-23316f2d7cf4%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei0sf76roYrwg1QdERquZE6DqMh9j686hJn45zU1FdBwuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django 1.6.11 model class query

2015-08-19 Thread Nikunj Badjatya
Also, 
When the same object is retrieved using 


print(Test.objects.get(id=row.id).uuid)  # Prints uuid.



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/75749d6e-f52e-4e36-97ec-23316f2d7cf4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Django 1.6.11 model class query

2015-08-19 Thread Nikunj Badjatya
Hello All,

I have a django model class defined as:


class Test(models.Model):
   id=AutoField(primary_key=True)
   name = models.CharField(max_length=45, null=False)
   description = models.CharField(max_length=200, null=True)
   uuid = models.CharField(max_length=250)
   class Meta:
   managed = False
db_table ="test"

Where, 
uuid is auto generated using a mysql trigger. 

Mysql Trigger looks like:
DROP TRIGGER IF EXISTS `test_uuid`//
CREATE TRIGGER `test_uuid` BEFORE INSERT ON` test
`
FOR EACH ROW begin if (new.uuid is  null or new.uuid = '') then set new.uuid 
= (select uuid());end if; end
//
This trigger is added at the db level.

i.e., any new insert in 'test' table would generate the value for uuid 
column.
and insert works fine for this model from the code.

Issue is, when I try to access the uuid after I have performed the save(), 
I am getting null.

row = Test(name=in_data['name'],
  description=in_data['description'],
   )
row.save()  # This creates a row with id, name, description and uuid (auto 
generated by the pre insert rigger)
print(row.id)   # Prints id
print(row.uuid) # Prints u' '




Any idea as to what may be going on?
Why row.uuid is coming as empty from the code though the uuid is clearly 
generated in the db table.


Any pointers on this would be highly appreciated.

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c46aa030-f3e0-4f0f-a671-2989e8cfcc52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.