Desired: Field behavior like AutoField + unique_with

2015-01-13 Thread Matt Cooper
I'm building a multi-tenant application with a data model that simplifies 
to:

class Tenant(models.Model):
name = models.CharField(max_length=50)

class Widget(models.Model):
owner = models.ForeignKey(Tenant)
sequence_id = 


I want Widget.sequence_id to work like an AutoField (that is, automatically 
give me something unique and generally incrementing by 1) but scoped to a 
single Widget.owner. So for instance, when Tenant A creates 3 widgets, they 
would get sequence_ids 1, 2, and 3. Then Tenant B comes along and creates 2 
widgets; those widgets get sequence_ids 1 and 2. The tenant/user gets no 
control over the number, but it's something she'll see in the UI. 
AutoNumber is out because from the perspective of a single tenant, they 
should have sequential widget IDs. GUIDs/UUIDs are out because it needs to 
be human-readable.

I looked through docs on AutoField and unique_together. I turned 
StackOverflow upside down and only found this 
, 
which isn't quite what I want. I didn't see anything on djangopackages.com 
that would solve my problem, though I could have missed it.

Basically, has anyone done something like this before and have a suggested 
pattern?

Thanks!
~matt

-- 
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/37fd32e7-28dd-4c10-8451-b0d705a3e52e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


ImportError: No module named check_constraints

2015-01-13 Thread Jenefa Jeyaraj
ImportError: No module named check_constraints

-- 
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/4d42cbdc-49ea-4f24-97f5-d7a2e0dc62b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Desired: Field behavior like AutoField + unique_with

2015-01-13 Thread Jani Tiainen
I've done something similiar.

Only way to do that is that you need to upkeep that tenant sequence_id manually,

So doing something like:

class Tenant(models.Model):
name = models.CharField(max_length=50)
sequence_value = models.IntegerField()

and in a code:

tenant = Tenant.object.get(id=tenant_id).select_for_update()  # Pessimistic 
locking
tenant.sequence_value = tenant.sequence_value + 1
tenant.save()

widget = Widget(owner=tenant,sequence_value=tenant.sequence_value)
widget.save()

That should do the trick. Another option could be using table triggers to 
automate
that within a database table trigger(s).

On Mon, 12 Jan 2015 21:26:54 -0800 (PST)
Matt Cooper  wrote:

> I'm building a multi-tenant application with a data model that simplifies 
> to:
> 
> class Tenant(models.Model):
> name = models.CharField(max_length=50)
> 
> class Widget(models.Model):
> owner = models.ForeignKey(Tenant)
> sequence_id = 
> 
> 
> I want Widget.sequence_id to work like an AutoField (that is, automatically 
> give me something unique and generally incrementing by 1) but scoped to a 
> single Widget.owner. So for instance, when Tenant A creates 3 widgets, they 
> would get sequence_ids 1, 2, and 3. Then Tenant B comes along and creates 2 
> widgets; those widgets get sequence_ids 1 and 2. The tenant/user gets no 
> control over the number, but it's something she'll see in the UI. 
> AutoNumber is out because from the perspective of a single tenant, they 
> should have sequential widget IDs. GUIDs/UUIDs are out because it needs to 
> be human-readable.
> 
> I looked through docs on AutoField and unique_together. I turned 
> StackOverflow upside down and only found this 
> , 
> which isn't quite what I want. I didn't see anything on djangopackages.com 
> that would solve my problem, though I could have missed it.
> 
> Basically, has anyone done something like this before and have a suggested 
> pattern?
> 
> Thanks!
> ~matt
> 
> -- 
> 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/37fd32e7-28dd-4c10-8451-b0d705a3e52e%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/20150113150642.3fd490a2%40jns42-l.w2k.keypro.fi.
For more options, visit https://groups.google.com/d/optout.


object has no attribute 'new_objects' error when saving related objects

2015-01-13 Thread mateja
I am working on a simple application to store sets of collected datapoints 
into a dataset. I am using the admin interface to handle CRUD management of 
the dataset objects. I would like to be able to add additional datapoints 
to an existing dataset instance by importing model data from a file. The 
error happens when I save the change form, I get the error "object has no 
attribute 'new_objects'"

This is what the admin change view looks like: http://imgur.com/gQGXquS

I have overridden the admin change view to include a file field that 
accepts a CSV file containing datapoint data. When the user clicks the Save 
button, the file should upload and the app should import and save 
datapoints from the file and associate them with the given dataset. I have 
googled around for this error and I have seen where others omitted to call 
the save_m2m() function, but I don't think this is what I am trying to do 
as I am not using a formset, I am using a file upload.

I have overridden the Dataset ModelAdmin save_model function to accept the 
file, save the datapoints, and associate them with the given dataset.

The code and the full error log is all here: 
https://gist.github.com/matejaputic/0e25cd74830033442886

Is this the right way to do this? I would appreciate any guidance.

Also, why are the existing datapoints being resubmitted? How can I prevent 
that from happening?

Thank you

-- 
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/b1f25527-1ebe-4506-b9b0-79a3a879a3ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Desired: Field behavior like AutoField + unique_with

2015-01-13 Thread Matt Cooper
Thanks Jani, good to hear someone has gone down this road before. Since you 
did it in code, is there a reason you kept a sequence value on Tenant 
rather than using Widget.objects.aggregate(Max(Widget.sequence_value)) ? I 
ask because for my app, Widget is actually just one of several cases where 
I need this pattern. If I could keep all the book-keeping on the model with 
the constraint, that seems cleaner to me. But, of course, I'm not willing 
to pay a huge performance or corruption penalty just for vague ideological 
purity :)

Thanks again!
~matt

On Tuesday, January 13, 2015 at 5:07:21 AM UTC-8, Jani Tiainen wrote:
>
> I've done something similiar. 
>
> Only way to do that is that you need to upkeep that tenant sequence_id 
> manually, 
>
> So doing something like: 
>
> class Tenant(models.Model): 
> name = models.CharField(max_length=50) 
> sequence_value = models.IntegerField() 
>
> and in a code: 
>
> tenant = Tenant.object.get(id=tenant_id).select_for_update()  # 
> Pessimistic locking 
> tenant.sequence_value = tenant.sequence_value + 1 
> tenant.save() 
>
> widget = Widget(owner=tenant,sequence_value=tenant.sequence_value) 
> widget.save() 
>
> That should do the trick. Another option could be using table triggers to 
> automate 
> that within a database table trigger(s). 
>
> On Mon, 12 Jan 2015 21:26:54 -0800 (PST) 
> Matt Cooper > wrote: 
>
> > I'm building a multi-tenant application with a data model that 
> simplifies 
> > to: 
> > 
> > class Tenant(models.Model): 
> > name = models.CharField(max_length=50) 
> > 
> > class Widget(models.Model): 
> > owner = models.ForeignKey(Tenant) 
> > sequence_id =  
> > 
> > 
> > I want Widget.sequence_id to work like an AutoField (that is, 
> automatically 
> > give me something unique and generally incrementing by 1) but scoped to 
> a 
> > single Widget.owner. So for instance, when Tenant A creates 3 widgets, 
> they 
> > would get sequence_ids 1, 2, and 3. Then Tenant B comes along and 
> creates 2 
> > widgets; those widgets get sequence_ids 1 and 2. The tenant/user gets no 
> > control over the number, but it's something she'll see in the UI. 
> > AutoNumber is out because from the perspective of a single tenant, they 
> > should have sequential widget IDs. GUIDs/UUIDs are out because it needs 
> to 
> > be human-readable. 
> > 
> > I looked through docs on AutoField and unique_together. I turned 
> > StackOverflow upside down and only found this 
> > , 
>
> > which isn't quite what I want. I didn't see anything on 
> djangopackages.com 
> > that would solve my problem, though I could have missed it. 
> > 
> > Basically, has anyone done something like this before and have a 
> suggested 
> > pattern? 
> > 
> > Thanks! 
> > ~matt 
> > 
> > -- 
> > 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...@googlegroups.com . 
> > To post to this group, send email to django...@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/37fd32e7-28dd-4c10-8451-b0d705a3e52e%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/1ab4b16d-df97-4412-9622-22cf3b344f80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: ImportError: No module named check_constraints

2015-01-13 Thread Babatunde Akinyanmi
I don't think you are going to get any helpful replies with that kind of
statement?
On 13 Jan 2015 13:04, "Jenefa Jeyaraj"  wrote:

> ImportError: No module named check_constraints
>
>  --
> 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/4d42cbdc-49ea-4f24-97f5-d7a2e0dc62b6%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/CA%2BWjgXPus81Pb35bO4OxBS4WC254NRvkw7XJWLNO_AO85UEc_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: passing variables

2015-01-13 Thread Scot Hacker


On Monday, January 12, 2015 at 5:29:08 PM UTC-8, suabiut wrote:
>
> How to you pass variable from one template to another template?
>

You haven't provided any information about your problem, but it sounds like 
you might want to read the docs on template inheritance:

https://docs.djangoproject.com/en/1.7/topics/templates/#template-inheritance

 

-- 
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/019d7274-0d52-4f7e-be9a-46f8335220af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


segmentation fault

2015-01-13 Thread Django User
hello
i keep getting the following error messages that state mod_wsgi was already 
loaded and segmentation faults. pls. see an example:

[Tue Jan 13 23:46:11 2015] [error] 
/usr/local/lib/python2.7/dist-packages/pytz/__init__.py:29: UserWarning: 
Module mod_wsgi was already imported from None, but 
/usr/local/lib/python2.7/dist-packages is being added to sys.path
[Tue Jan 13 23:46:11 2015] [error]   from pkg_resources import 
resource_stream
[Tue Jan 13 23:46:12 2015] [notice] child pid 11979 exit signal 
Segmentation fault (11)
[Tue Jan 13 23:46:12 2015] [notice] child pid 11993 exit signal 
Segmentation fault (11)

how do i go about resolving this? 

kindly let me know.

thanks in advance.




-- 
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/5839f545-39e6-48dd-a441-263ccbcf3693%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: segmentation fault

2015-01-13 Thread Xavier Ordoquy

> Le 13 janv. 2015 à 19:25, Django User  a écrit :
> 
> hello
> i keep getting the following error messages that state mod_wsgi was already 
> loaded and segmentation faults. pls. see an example:
> 
> [Tue Jan 13 23:46:11 2015] [error] 
> /usr/local/lib/python2.7/dist-packages/pytz/__init__.py:29: UserWarning: 
> Module mod_wsgi was already imported from None, but 
> /usr/local/lib/python2.7/dist-packages is being added to sys.path
> [Tue Jan 13 23:46:11 2015] [error]   from pkg_resources import resource_stream
> [Tue Jan 13 23:46:12 2015] [notice] child pid 11979 exit signal Segmentation 
> fault (11)
> [Tue Jan 13 23:46:12 2015] [notice] child pid 11993 exit signal Segmentation 
> fault (11)
> 
> how do i go about resolving this? 

You need to ensure that your Python/C libraries are up to date with your system.

Regards,
Xavier Ordoquy,
Linovia.

> kindly let me know.
> 
> thanks in advance.
> 
> 
> 
> 
> 
> -- 
> 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/5839f545-39e6-48dd-a441-263ccbcf3693%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/28B9D850-1F38-4C20-B19D-681F1772FF9B%40linovia.com.
For more options, visit https://groups.google.com/d/optout.


[ANNOUNCE] Security releases issued (1.4.18, 1.6.10, 1.7.3)

2015-01-13 Thread Tim Graham
Today the Django team is issuing multiple releases -- Django 1.4.18, Django 
1.6.10, and Django 1.7.3 -- as part of our security process. These releases 
address several security issues, and we encourage all users to upgrade as 
soon as possible.

More details can be found on our blog:

https://www.djangoproject.com/weblog/2015/jan/13/security/

As a reminder, we ask that potential security issues be reported via 
private email to secur...@djangoproject.com, and not via Django's Trac 
instance or the django-developers list. Please see 
https://www.djangoproject.com/security for further information.

-- 
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/6d8ec199-c011-422a-a268-cb8aeed4d402%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


hello need help to configure django

2015-01-13 Thread zsandrusha
I had worked site @ webfaction hosting but i loose old domain names and 
trying to configure new domain name, here what i see on screenshot:
http://joxi.ru/Y2LyjZecxPeOA6

realy need urgent help

-- 
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/b1fee3ba-cb9b-4539-b631-44a63a7853e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


need help to configure new domain

2015-01-13 Thread zsandrusha
hello i need urgent help to configure django,
i had site on django cms then i loose my old domaind now i need to add new 
domain name but i see - http://joxi.ru/82QDVneI8o3amd
so i realy need help to configure my website with fee

-- 
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/60ad553d-0a20-47f3-881a-5e9165f68063%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: python with django orm code , after exits, the entry from db revert backs

2015-01-13 Thread JAI PRAKASH SINGH
no sir , im really sorry actually  there was some bug from my side. but
thank you sir for response, at i know some concept about autocommit.

On Mon, Jan 12, 2015 at 2:12 PM, Sergiy Khohlov  wrote:

> Have you enabled autocommit to yes?
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/MbENyxvFMh4/unsubscribe.
> To unsubscribe from this group and all its topics, 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/CADTRxJM1w%3DJKpR5DtEmWEyOtRiQoBxwYQgdRCY0t8QmGBixuWA%40mail.gmail.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/CAGtuQriSm_VxZbqnS%3DS6%2BG8udVWN7GARt%3DzoB7LG6Kx8CvJhiw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


do migrations (Django 1.7+) handle production data?

2015-01-13 Thread Abraham Varricatt
Hello everyone,

One of the biggest features introduced in Django 1.7 are migrations. They 
can broadly be classified into 2 types - 
* schema migrations
* data migrations

Schema migrations deal with changes to the database schema. eg - changing 
max_digits of a DecimalField.
Data migrations revolve around the actual data in your database and are not 
automatically taken care of.

My question is; 

Why is there so much praise for the Django (1.7) migrations feature if it 
can't handle data? Yes, I can understand the convenience of schema 
migrations, but without it being accompanied by data migrations, what's the 
point? (I'm assuming that this is something run on a production 
environment. For test/development environments, the data can arguably be 
worthless)

Puzzled,
Abraham V.

-- 
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/fb4d2132-6f47-4ebf-afb8-2822c20716ea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: do migrations (Django 1.7+) handle production data?

2015-01-13 Thread Russell Keith-Magee
On Wed, Jan 14, 2015 at 3:03 PM, Abraham Varricatt <
abraham.varric...@googlemail.com> wrote:

> Hello everyone,
>
> One of the biggest features introduced in Django 1.7 are migrations. They
> can broadly be classified into 2 types -
> * schema migrations
> * data migrations
>
> Schema migrations deal with changes to the database schema. eg - changing
> max_digits of a DecimalField.
> Data migrations revolve around the actual data in your database and are
> not automatically taken care of.
>
> My question is;
>
> Why is there so much praise for the Django (1.7) migrations feature if it
> can't handle data? Yes, I can understand the convenience of schema
> migrations, but without it being accompanied by data migrations, what's the
> point? (I'm assuming that this is something run on a production
> environment. For test/development environments, the data can arguably be
> worthless)
>

What gave you the impression that Django's migrations doesn't handle data?
There's a section in the Migration topic guide in Django's documentation
entitled "Data Migrations":

https://docs.djangoproject.com/en/1.7/topics/migrations/#data-migrations

Yours,
Russ Magee %-)

-- 
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/CAJxq848Rz1kLjFjUoBs6gASNd1QzKDBGj%3DnY1O-QoLmSkzfwtQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Desired: Field behavior like AutoField + unique_with

2015-01-13 Thread Jani Tiainen
Because sequence number was running number per "tenant" (in my case it
was invoice type) that started from zero (0) at the beginning of the
every year.

You could use max+1 as well but you should be aware that there is a
slim chance that there is race condition and you may end up having two
entities with the same max number - that though would raise error when
trying to save into database.

By having explicitly pessimistic locking (select_for_update) for a row
with tenant + sequence value you can guarantee that no other request
can change the value while you're reading it.

To make that easier you can create custom manager that can retrieve
updated sequence values automagically, so your call would look like
something like:

Tenant.sequence.get_value(tenant, sequence)


On Tue, 13 Jan 2015 06:37:21 -0800 (PST)
Matt Cooper  wrote:

> Thanks Jani, good to hear someone has gone down this road before.
> Since you did it in code, is there a reason you kept a sequence value
> on Tenant rather than using
> Widget.objects.aggregate(Max(Widget.sequence_value)) ? I ask because
> for my app, Widget is actually just one of several cases where I need
> this pattern. If I could keep all the book-keeping on the model with
> the constraint, that seems cleaner to me. But, of course, I'm not
> willing to pay a huge performance or corruption penalty just for
> vague ideological purity :)
> 
> Thanks again!
> ~matt
> 
> On Tuesday, January 13, 2015 at 5:07:21 AM UTC-8, Jani Tiainen wrote:
> >
> > I've done something similiar. 
> >
> > Only way to do that is that you need to upkeep that tenant
> > sequence_id manually, 
> >
> > So doing something like: 
> >
> > class Tenant(models.Model): 
> > name = models.CharField(max_length=50) 
> > sequence_value = models.IntegerField() 
> >
> > and in a code: 
> >
> > tenant = Tenant.object.get(id=tenant_id).select_for_update()  # 
> > Pessimistic locking 
> > tenant.sequence_value = tenant.sequence_value + 1 
> > tenant.save() 
> >
> > widget = Widget(owner=tenant,sequence_value=tenant.sequence_value) 
> > widget.save() 
> >
> > That should do the trick. Another option could be using table
> > triggers to automate 
> > that within a database table trigger(s). 
> >
> > On Mon, 12 Jan 2015 21:26:54 -0800 (PST) 
> > Matt Cooper > wrote: 
> >
> > > I'm building a multi-tenant application with a data model that 
> > simplifies 
> > > to: 
> > > 
> > > class Tenant(models.Model): 
> > > name = models.CharField(max_length=50) 
> > > 
> > > class Widget(models.Model): 
> > > owner = models.ForeignKey(Tenant) 
> > > sequence_id =  
> > > 
> > > 
> > > I want Widget.sequence_id to work like an AutoField (that is, 
> > automatically 
> > > give me something unique and generally incrementing by 1) but
> > > scoped to 
> > a 
> > > single Widget.owner. So for instance, when Tenant A creates 3
> > > widgets, 
> > they 
> > > would get sequence_ids 1, 2, and 3. Then Tenant B comes along and 
> > creates 2 
> > > widgets; those widgets get sequence_ids 1 and 2. The tenant/user
> > > gets no control over the number, but it's something she'll see in
> > > the UI. AutoNumber is out because from the perspective of a
> > > single tenant, they should have sequential widget IDs.
> > > GUIDs/UUIDs are out because it needs 
> > to 
> > > be human-readable. 
> > > 
> > > I looked through docs on AutoField and unique_together. I turned 
> > > StackOverflow upside down and only found this 
> > > , 
> >
> > > which isn't quite what I want. I didn't see anything on 
> > djangopackages.com 
> > > that would solve my problem, though I could have missed it. 
> > > 
> > > Basically, has anyone done something like this before and have a 
> > suggested 
> > > pattern? 
> > > 
> > > Thanks! 
> > > ~matt 
> > > 
> > > -- 
> > > 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...@googlegroups.com . 
> > > To post to this group, send email to django...@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/37fd32e7-28dd-4c10-8451-b0d705a3e52e%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/1ab4b16d-df97-4412-9622-22cf3