How to separate data having the same model but belonging to different users?
Hi I am a django #n00b. I came across the django model documentation and found it pretty interesting. ( https://docs.djangoproject.com/en/dev/topics/db/models/). Now my usecase requires I have a set of Models and each model has multiple tables corresponding to it. For example when user1 registers I can create a table user1_t1, user1_t2, user1_t3. When user2 registers I can create a table user2_t1, user2_t2, user2_t3. I really like the Model abstraction but can't find a way to create these multiple tables conveniently without creating new models. I could not find clear solutions to this on the internet. I just want clear separation between t1, t2, t3 for the all users. Depending on the logged in user, I want to use the relevant table. What is the cleanest way to achieve this with Django? If it is not possible to do this with tables I can think about different databases one for each user with the same set of tables. Is it possible to do the same with multiple databases? Thanks Rohit Banga -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/tl8qKhJb-_cJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to separate data having the same model but belonging to different users?
Just HAVE to separate data - requirement. On Sep 21, 2012 1:59 PM, "Mayukh Mukherjee" wrote: > As I understand it: (And im fairly new to django too) > > A model corresponds to a single table (not multiple). > The question to you is what is different between User1 and User2 that you > need different tables? > > > > On Fri, Sep 21, 2012 at 1:35 PM, Rohit Banga wrote: > >> Hi >> >> I am a django #n00b. I came across the django model documentation and >> found it pretty interesting. ( >> https://docs.djangoproject.com/en/dev/topics/db/models/). >> >> Now my usecase requires I have a set of Models and each model has >> multiple tables corresponding to it. >> For example when user1 registers I can create a table user1_t1, user1_t2, >> user1_t3. >> When user2 registers I can create a table user2_t1, user2_t2, user2_t3. >> >> I really like the Model abstraction but can't find a way to create these >> multiple tables conveniently without creating new models. I could not find >> clear solutions to this on the internet. >> I just want clear separation between t1, t2, t3 for the all users. >> Depending on the logged in user, I want to use the relevant table. What is >> the cleanest way to achieve this with Django? >> >> If it is not possible to do this with tables I can think about different >> databases one for each user with the same set of tables. Is it possible to >> do the same with multiple databases? >> >> Thanks >> Rohit Banga >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/django-users/-/tl8qKhJb-_cJ. >> To post to this group, send email to django-users@googlegroups.com. >> To unsubscribe from this group, send email to >> django-users+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/django-users?hl=en. >> > > > > -- > Mayukh Mukherjee > http://www.linkedin.com/in/mayukhmmukherjee > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to separate data having the same model but belonging to different users?
Thanks for your comments. I agree that technically it is feasible to achieve the same affect with row level permissions or filtering the rows by user. The requirement is to keep the data separate using different tables, databases while still using the same model. May be user is not the right metaphor to use here. Lets just "imagine" that we have the same schema to use for different departments in a college but one department does not want to house their data in the same tables as another department. I don't want to filter rows by "userid" since one place we forget the filter in the code and there is an unauthorized data access. I will look into dynamic models though I am not sure if it is well supported. What about routing to different databases based on user id? Thanks Rohit Banga http://iamrohitbanga.com/ On Fri, Sep 21, 2012 at 2:34 PM, Joel Goldstick wrote: > On Fri, Sep 21, 2012 at 2:30 PM, Nikolas Stevenson-Molnar > wrote: > > If you absolutely have to use separate tables per user (again, I do not > > recommend this), then you'll need to implement some form of dynamic > models > > (models which can be constructed at run-time rather than needing to be > > defined in the application code) such as discussed here: > > https://code.djangoproject.com/wiki/DynamicModels (see link at the top > of > > the page for newer approaches and full implementations of dynamic > models). > > > > > > _Nik > > > > On 9/21/2012 11:07 AM, Rohit Banga wrote: > > > > Just HAVE to separate data - requirement. > > > > On Sep 21, 2012 1:59 PM, "Mayukh Mukherjee" wrote: > >> > >> As I understand it: (And im fairly new to django too) > >> > >> A model corresponds to a single table (not multiple). > >> The question to you is what is different between User1 and User2 that > you > >> need different tables? > >> > >> > >> > >> On Fri, Sep 21, 2012 at 1:35 PM, Rohit Banga > >> wrote: > >>> > >>> Hi > >>> > >>> I am a django #n00b. I came across the django model documentation and > >>> found it pretty interesting. > >>> (https://docs.djangoproject.com/en/dev/topics/db/models/). > >>> > >>> Now my usecase requires I have a set of Models and each model has > >>> multiple tables corresponding to it. > >>> For example when user1 registers I can create a table user1_t1, > user1_t2, > >>> user1_t3. > >>> When user2 registers I can create a table user2_t1, user2_t2, user2_t3. > >>> > >>> I really like the Model abstraction but can't find a way to create > these > >>> multiple tables conveniently without creating new models. I could not > find > >>> clear solutions to this on the internet. > >>> I just want clear separation between t1, t2, t3 for the all users. > >>> Depending on the logged in user, I want to use the relevant table. > What is > >>> the cleanest way to achieve this with Django? > >>> > >>> If it is not possible to do this with tables I can think about > different > >>> databases one for each user with the same set of tables. Is it > possible to > >>> do the same with multiple databases? > >>> > >>> Thanks > >>> Rohit Banga > >>> -- > > Why not add a user as a field in your models. Then, when the user > logs in, make sure the queries filter only that user's data > > -- > Joel Goldstick > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to separate data having the same model but belonging to different users?
I just saw this example: http://django.readthedocs.org/en/1.4/topics/db/models.html#multi-table-inheritance Since it is possible for me to have a few number of users (now called departments), I can define a create a python file which subclasses all the models and then run syncdb to update the database for creating the new tables. But what is different is I need to fetch the Place subclass (eg. Restaurant or School page above) at runtime based on the logged in user. Assuming I have a map from id to class name can I just load it dynamically? Thanks Rohit Banga http://iamrohitbanga.com/ On Fri, Sep 21, 2012 at 3:26 PM, Rohit Banga wrote: > Thanks for your comments. I agree that technically it is feasible to > achieve the same affect with row level permissions or filtering the rows by > user. > The requirement is to keep the data separate using different tables, > databases while still using the same model. May be user is not the right > metaphor to use here. Lets just "imagine" that we have the same schema to > use for different departments in a college but one department does not want > to house their data in the same tables as another department. > > I don't want to filter rows by "userid" since one place we forget the > filter in the code and there is an unauthorized data access. > > I will look into dynamic models though I am not sure if it is well > supported. > What about routing to different databases based on user id? > > Thanks > Rohit Banga > http://iamrohitbanga.com/ > > > > On Fri, Sep 21, 2012 at 2:34 PM, Joel Goldstick > wrote: > >> On Fri, Sep 21, 2012 at 2:30 PM, Nikolas Stevenson-Molnar >> wrote: >> > If you absolutely have to use separate tables per user (again, I do not >> > recommend this), then you'll need to implement some form of dynamic >> models >> > (models which can be constructed at run-time rather than needing to be >> > defined in the application code) such as discussed here: >> > https://code.djangoproject.com/wiki/DynamicModels (see link at the top >> of >> > the page for newer approaches and full implementations of dynamic >> models). >> > >> > >> > _Nik >> > >> > On 9/21/2012 11:07 AM, Rohit Banga wrote: >> > >> > Just HAVE to separate data - requirement. >> > >> > On Sep 21, 2012 1:59 PM, "Mayukh Mukherjee" wrote: >> >> >> >> As I understand it: (And im fairly new to django too) >> >> >> >> A model corresponds to a single table (not multiple). >> >> The question to you is what is different between User1 and User2 that >> you >> >> need different tables? >> >> >> >> >> >> >> >> On Fri, Sep 21, 2012 at 1:35 PM, Rohit Banga >> >> wrote: >> >>> >> >>> Hi >> >>> >> >>> I am a django #n00b. I came across the django model documentation and >> >>> found it pretty interesting. >> >>> (https://docs.djangoproject.com/en/dev/topics/db/models/). >> >>> >> >>> Now my usecase requires I have a set of Models and each model has >> >>> multiple tables corresponding to it. >> >>> For example when user1 registers I can create a table user1_t1, >> user1_t2, >> >>> user1_t3. >> >>> When user2 registers I can create a table user2_t1, user2_t2, >> user2_t3. >> >>> >> >>> I really like the Model abstraction but can't find a way to create >> these >> >>> multiple tables conveniently without creating new models. I could not >> find >> >>> clear solutions to this on the internet. >> >>> I just want clear separation between t1, t2, t3 for the all users. >> >>> Depending on the logged in user, I want to use the relevant table. >> What is >> >>> the cleanest way to achieve this with Django? >> >>> >> >>> If it is not possible to do this with tables I can think about >> different >> >>> databases one for each user with the same set of tables. Is it >> possible to >> >>> do the same with multiple databases? >> >>> >> >>> Thanks >> >>> Rohit Banga >> >>> -- >> >> Why not add a user as a field in your models. Then, when the user >> logs in, make sure the queries filter only that user's data >> >> -- >> Joel Goldstick >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To post to this group, send email to django-users@googlegroups.com. >> To unsubscribe from this group, send email to >> django-users+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/django-users?hl=en. >> >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to separate data having the same model but belonging to different users?
Sounds good Joel... Thanks. What if I want to dynamically create the object of the subclass so that I don't have to duplicate the code. If I have class Department(models.Model): someField =... class PhysicsDepartment(Department): pass Then how do I instantiate the objects dynamically so I do not have to do Department.objects.filter() PhysicsDepartment.objects.filter() I want to share the same code for all departments. Can I instantiate them dynamically given that I have the string name and the module name available in a dictionary. Thanks Rohit Banga http://iamrohitbanga.com/ On Fri, Sep 21, 2012 at 4:16 PM, Joel Goldstick wrote: > On Fri, Sep 21, 2012 at 4:05 PM, Rohit Banga > wrote: > > I just saw this example: > > > http://django.readthedocs.org/en/1.4/topics/db/models.html#multi-table-inheritance > > > > Since it is possible for me to have a few number of users (now called > > departments), I can define a create a python file which subclasses all > the > > models and then run syncdb to update the database for creating the new > > tables. > > But what is different is I need to fetch the Place subclass (eg. > Restaurant > > or School page above) at runtime based on the logged in user. Assuming I > > have a map from id to class name can I just load it dynamically? > > > > Thanks > > Rohit Banga > > http://iamrohitbanga.com/ > > > > > > On Fri, Sep 21, 2012 at 3:26 PM, Rohit Banga > > wrote: > >> > >> Thanks for your comments. I agree that technically it is feasible to > >> achieve the same affect with row level permissions or filtering the > rows by > >> user. > >> The requirement is to keep the data separate using different tables, > >> databases while still using the same model. May be user is not the right > >> metaphor to use here. Lets just "imagine" that we have the same schema > to > >> use for different departments in a college but one department does not > want > >> to house their data in the same tables as another department. > >> > >> I don't want to filter rows by "userid" since one place we forget the > >> filter in the code and there is an unauthorized data access. > >> > >> I will look into dynamic models though I am not sure if it is well > >> supported. > >> What about routing to different databases based on user id? > >> > >> Thanks > >> Rohit Banga > >> http://iamrohitbanga.com/ > >> > >> > >> > >> On Fri, Sep 21, 2012 at 2:34 PM, Joel Goldstick < > joel.goldst...@gmail.com> > >> wrote: > >>> > >>> On Fri, Sep 21, 2012 at 2:30 PM, Nikolas Stevenson-Molnar > >>> wrote: > >>> > If you absolutely have to use separate tables per user (again, I do > not > >>> > recommend this), then you'll need to implement some form of dynamic > >>> > models > >>> > (models which can be constructed at run-time rather than needing to > be > >>> > defined in the application code) such as discussed here: > >>> > https://code.djangoproject.com/wiki/DynamicModels (see link at the > top > >>> > of > >>> > the page for newer approaches and full implementations of dynamic > >>> > models). > >>> > > >>> > > >>> > _Nik > >>> > > >>> > On 9/21/2012 11:07 AM, Rohit Banga wrote: > >>> > > >>> > Just HAVE to separate data - requirement. > >>> > > >>> > On Sep 21, 2012 1:59 PM, "Mayukh Mukherjee" > wrote: > >>> >> > >>> >> As I understand it: (And im fairly new to django too) > >>> >> > >>> >> A model corresponds to a single table (not multiple). > >>> >> The question to you is what is different between User1 and User2 > that > >>> >> you > >>> >> need different tables? > >>> >> > >>> >> > >>> >> > >>> >> On Fri, Sep 21, 2012 at 1:35 PM, Rohit Banga < > iamrohitba...@gmail.com> > >>> >> wrote: > >>> >>> > >>> >>> Hi > >>> >>> > >>> >>> I am a django #n00b. I came across the django model documentation > and > >>> >>> found it pretty interesting. > >>> >>> (https://docs.djangoproject.com/en/dev/topics/db/models/). > >>> &
Re: How to separate data having the same model but belonging to different users?
Sure Nikolas I will reconsider your solution. In case I go for model inheritance then can I use the following solution to load the class dynamically? mod = __import__('mysite.departments', fromlist=[form.getDepartment()]) klass = getattr(mod, 'form.getDepartment()') Thanks Rohit Banga http://iamrohitbanga.com/ On Fri, Sep 21, 2012 at 4:33 PM, Nikolas Stevenson-Molnar < nik.mol...@consbio.org> wrote: > I would still argue that the best solution is to use a robust permissions > model which would preclude this. Wherever there is code, you invariably > have the potential for security flaws. The more complicated you make that > code, the more chances for mistakes. On the other hand, simpler code with > well-defined methods for data access (e.g., maybe you never use > MyModel.objects, but rather have a custom function for filtering objects > based on permissions constraints; then you only have to ensure security in > one place) make for fewer mistakes and a code base which is easier to > maintain. > > _Nik > > > On 9/21/2012 12:26 PM, Rohit Banga wrote: > > > I don't want to filter rows by "userid" since one place we forget the > filter in the code and there is an unauthorized data access. > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to separate data having the same model but belonging to different users?
Thanks Nikolas. I think my example was not clear. But all the code is shared between the departments (another reason for you to say use the same tables!). I do not need to have the explicit department name in the code. I don't know the name of the departments yet! (It is just a metaphor) I just want to add behavior like PhysicsDepartment.objects.filter() or create(), save() anywhere I want. I want to work with the base class while loading the data from the subclass at runtime. Simple polymorphism but with different database tables in the backend. Thanks Rohit Banga http://iamrohitbanga.com/ On Fri, Sep 21, 2012 at 4:55 PM, Nikolas Stevenson-Molnar < nik.mol...@consbio.org> wrote: > If I understand correctly, that's not the type of dynamic loading you > need. That statement can be the much simpler: > > >>> from mysite.departments.form import getDepartment > > Rather, if you need models (tables) mapped to users at runtime, you need > to load the *those* dynamically (normally you would define the model in > your code, which includes--either implicitly or explicitly--the table name). > > At the risk of sounding like a broken record: simpler is better, and > multiple, dynamically loaded models with the same schema is *not* simple > ;) > > _Nik > > > On 9/21/2012 1:43 PM, Rohit Banga wrote: > > Sure Nikolas I will reconsider your solution. > In case I go for model inheritance then can I use the following solution > to load the class dynamically? > > mod = __import__('mysite.departments', fromlist=[form.getDepartment()]) > > > klass = getattr(mod, 'form.getDepartment()') > > > Thanks > Rohit Banga > http://iamrohitbanga.com/ > > > On Fri, Sep 21, 2012 at 4:33 PM, Nikolas Stevenson-Molnar < > nik.mol...@consbio.org> wrote: > >> I would still argue that the best solution is to use a robust >> permissions model which would preclude this. Wherever there is code, you >> invariably have the potential for security flaws. The more complicated you >> make that code, the more chances for mistakes. On the other hand, simpler >> code with well-defined methods for data access (e.g., maybe you never use >> MyModel.objects, but rather have a custom function for filtering objects >> based on permissions constraints; then you only have to ensure security in >> one place) make for fewer mistakes and a code base which is easier to >> maintain. >> >> _Nik >> >> >> On 9/21/2012 12:26 PM, Rohit Banga wrote: >> >> >> I don't want to filter rows by "userid" since one place we forget the >> filter in the code and there is an unauthorized data access. >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To post to this group, send email to django-users@googlegroups.com. >> To unsubscribe from this group, send email to >> django-users+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/django-users?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to separate data having the same model but belonging to different users?
Hi Dennis Thanks for summarizing the contents of the mails. Do you foresee any problems with the Model Inheritance scheme? At my end I am stuck at getting a way to achieve dynamic polymorphism - which is probably because I am new to Python. I can create a new subclass for every Department I add. manage.py syncdb creates the new table for every new department I add. Now I want to write the application logic agnostic of the specific department. I want to load the appropriate subclass dynamically at runtime. I haven't been able to do this right now. I can afford to create a new subclass everytime and run manage.py syndb after that but cannot rewrite all the application logic all over again. I feel given the subclass name there should be an easy way in python to get the subclass itself which I should use for the current user. At this point I should also mention there are a set of about 5-6 tables (department being just one example) that I need to replicate for each new department. I am thinking of doing it all via subclassing. That is I would need to load as many subclass objects dynamically given the name of the department. If I can get the set of subclasses to use with a not too ugly looking code is it still a terrible idea? Thanks Rohit Banga http://iamrohitbanga.com/ On Fri, Sep 21, 2012 at 9:34 PM, Dennis Lee Bieber wrote: > On Fri, 21 Sep 2012 17:54:06 -0400, Rohit Banga > declaimed the following in > gmane.comp.python.django.user: > > > Thanks Nikolas. I think my example was not clear. > > > > But all the code is shared between the departments (another reason for > you > > to say use the same tables!). > > I do not need to have the explicit department name in the code. I don't > > know the name of the departments yet! (It is just a metaphor) > > I just want to add behavior like PhysicsDepartment.objects.filter() or > > create(), save() anywhere I want. > > I want to work with the base class while loading the data from the > subclass > > at runtime. Simple polymorphism but with different database tables in the > > backend. > > > > I expect that over 95% of the responses will all emphasize using > single set of tables, and a filter by the department (which is retrieved > from the log-in authorization information) > > Anything else means you have to somehow dynamically: > > 1) use one database for authorization and; somehow OPEN a department > database connection that the models will hook into instead of using a > "load-time" database. That probably means you have to replace the Django > database connection system with one that you call at run-time. IOW, you > do not have a database configured in the normal manner -- but EVERY > Django function that issues an SQL operation would have to do something > like: > > if not dbConnection: > dbName = authuserdepartmentname > dbConnection = whateverapi(database=dbName,...) > > where dbConnection is whatever Django normally uses to track the > database connection. > > > 2) not use Django models defined at build time (ie, in code), but > dynamically build the models at run-time so that you can specify the > table name when accessed (sort of using code templates into which you > substitute the needed table names which are then imported later -- good > luck getting Django to recognize the validity of the models). > > > 3) operate separate "servers" for each department. Each server would > have its own location for datafiles, but you can probably share the > application code via soft-links or maybe even one shared code directory. > This is workable for those databases that allow connections defined by > file name/path (SQLite, embedded Firebird) but not for those using a > dedicated database server (MySQL, PostgreSQL) -- because the file "name" > will be fixed, but the data path would be relative to the "web server" > URL. Of course, if you have many departments, you end up with many > "servers" on different ports: > > http://server.localhost:8080/ Physics > http://server.localhost:8081/ Mathematics > > or your server on port 80 rewrites URLs > > http://physics.server.localhost/ => server.localhost:8080 > http://mathematics.server.localhost/ => server.localhost:8081 > or > http://physics.server.localhost/ => > http://server.localhost/physics > > where the directory structure is something like > > physics/ > app1-> softlink to common/app1 > data/ > sqlite.db > > mathematics/ > app1-> softlink to common/app1 > data/ >
Re: How to separate data having the same model but belonging to different users?
Thats interesting Bill. Are you talking about something like this? http://stackoverflow.com/questions/3276700/django-model-subclass-without-changing-database-name Thanks Rohit Banga http://iamrohitbanga.com/ On Sat, Sep 22, 2012 at 8:15 PM, Bill Beal wrote: > Question for an expert from a newbie: Could you decorate each model that > has data that needs to be separated by departments? If so, you wouldn't > have to remember to code the department filter on each retrieval. > > > On Friday, September 21, 2012 10:06:35 PM UTC-4, Rohit Banga wrote: > >> Hi Dennis >> Thanks for summarizing the contents of the mails. >> Do you foresee any problems with the Model Inheritance scheme? At my end >> I am stuck at getting a way to achieve dynamic polymorphism - which is >> probably because I am new to Python. >> >> I can create a new subclass for every Department I add. manage.py syncdb >> creates the new table for every new department I add. Now I want to >> write the application logic agnostic of the specific department. I want to >> load the appropriate subclass dynamically at runtime. I haven't been able >> to do this right now. >> I can afford to create a new subclass everytime and run manage.py syndb >> after that but cannot rewrite all the application logic all over again. >> >> I feel given the subclass name there should be an easy way in python to >> get the subclass itself which I should use for the current user. >> >> At this point I should also mention there are a set of about 5-6 tables >> (department being just one example) that I need to replicate for each new >> department. I am thinking of doing it all via subclassing. That is I would >> need to load as many subclass objects dynamically given the name of the >> department. >> If I can get the set of subclasses to use with a not too ugly looking >> code is it still a terrible idea? >> >> Thanks >> Rohit Banga >> http://iamrohitbanga.com/ >> >> >> On Fri, Sep 21, 2012 at 9:34 PM, Dennis Lee Bieber >> wrote: >> >>> On Fri, 21 Sep 2012 17:54:06 -0400, Rohit Banga >>> declaimed the following in >>> gmane.comp.python.django.user: >>> >>> > Thanks Nikolas. I think my example was not clear. >>> > >>> > But all the code is shared between the departments (another reason for >>> you >>> > to say use the same tables!). >>> > I do not need to have the explicit department name in the code. I don't >>> > know the name of the departments yet! (It is just a metaphor) >>> > I just want to add behavior like PhysicsDepartment.objects.**filter() >>> or >>> > create(), save() anywhere I want. >>> > I want to work with the base class while loading the data from the >>> subclass >>> > at runtime. Simple polymorphism but with different database tables in >>> the >>> > backend. >>> > >>> >>> I expect that over 95% of the responses will all emphasize using >>> single set of tables, and a filter by the department (which is retrieved >>> from the log-in authorization information) >>> >>> Anything else means you have to somehow dynamically: >>> >>> 1) use one database for authorization and; somehow OPEN a department >>> database connection that the models will hook into instead of using a >>> "load-time" database. That probably means you have to replace the Django >>> database connection system with one that you call at run-time. IOW, you >>> do not have a database configured in the normal manner -- but EVERY >>> Django function that issues an SQL operation would have to do something >>> like: >>> >>> if not dbConnection: >>> dbName = authuserdepartmentname >>> dbConnection = whateverapi(database=dbName,..**.) >>> >>> where dbConnection is whatever Django normally uses to track the >>> database connection. >>> >>> >>> 2) not use Django models defined at build time (ie, in code), but >>> dynamically build the models at run-time so that you can specify the >>> table name when accessed (sort of using code templates into which you >>> substitute the needed table names which are then imported later -- good >>> luck getting Django to recognize the validity of the models). >>> >>> >>> 3) operate separate "servers" for each department. Each server would >>> have i
Re: How to separate data having the same model but belonging to different users?
How about maintaining one database per department and then using "using" parameter to select the appropriate database? There are not too many departments. I know it may not scale or seem elegant but keeping data in separate tables or databases is a requirement. Using the following command I feel I can sync all models to a new database. ./manage.py syncdb --database=department1 Do you foresee any problem with this solution? I am planning to have a default database with all users and groups and then based on the group select the appropriate database. I do not want to sync django-admin tables to all databases. Is there a way to restrict models to a specific database? Let say I have databases 0 ... n. Departments 1 to n have the same set of tables with different data and database i corresponds to department i. database 0 is used to store only admin database tables. Is there a reasonable way to restrict this within the same app or are there problems that I cannot foresee? Thanks Rohit Banga http://iamrohitbanga.com/ On Sun, Sep 23, 2012 at 12:56 PM, Dennis Lee Bieber wrote: > On Sat, 22 Sep 2012 22:04:41 -0700 (PDT), Bill Beal > declaimed the following in > gmane.comp.python.django.user: > > > > > I should think that if you have a foreign key to the department in all > > tables that must be segregated by department, you could create a > decorator > > that would apply a filter with the department key. But I don't know how > to > > do it. > > > Which basically puts it back to what the OP insists they are not > permitted to do -- have a single set of tables with a selection based on > the logged in user's department. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/ > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Problem with URL configuration - cannot use absolute URLs in my case
Hi I have a website at example.com/mywebsite. All URL requests directed to this URL are directed to my app. Now I have the following project structure mywebsiteroot/ -- myapp/ -- manage.py -- myapp/ --templates/ --settings.py --models.py --views.py When my base.html template includes a link "mylink" which shows some information. On the home page this works fine. my django app receives a URL request for example.com/mywebsite/mylink which I serve using base.html template. Now I extend the base.html template elsewhere in another view mylink/. In that view the link points to example.com/mywebsite/mylink/mylink which is obviously wrong. If I use an absolute link /mylink; I get a request at example.com/mylink and not example.com/mywebsite/mylink which is also wrong. So how do I code my url conf / view / template in order to be independent of mywebsite name. Is there anything wrong with the way I am wiring up my application. Thanks in Advance! -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/pAvBYC2INMUJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
AttributeError for unique field during ModelForm validation
I noticed a strange behavior with Django and filed a bug https://code.djangoproject.com/ticket/19271#ticket It is suggested that I first check on the support group if the bug is valid or not. Fair Enough. I have created a standalone project to demonstrate the problem. In order to run it you may have to create a database and configure it in settings.py though. Code that does not work: https://github.com/iamrohitbanga/django_ticket_19271/tree/code_not_working Code that works: https://github.com/iamrohitbanga/django_ticket_19271/tree/code_working Just creating my_id field in BaseModel class with unique=True set causes the code to fail with the stack trace below. My usecase is a bit weird hence you would find model form inheriting from abstract models. IMHO that should not matter. The code works without unique fields but does not work with non-unique fields. I will be happy to provide more clarifications. Below is the stacktrace for the "AttributeError at /create_new type object 'BaseModel' has no attribute '_default_manager' Traceback: File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "django_bug_19271/testproj/testproj/views.py" in create_new 33. if form.is_valid(): File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in is_valid 124. return self.is_bound and not bool(self.errors) File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _get_errors 115. self.full_clean() File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in full_clean 272. self._post_clean() File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _post_clean 338. self.validate_unique() File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in validate_unique 347. self.instance.validate_unique(exclude=exclude) File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in validate_unique 633. errors = self._perform_unique_checks(unique_checks) File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _perform_unique_checks 717. qs = model_class._default_manager.filter(**lookup_kwargs) Exception Type: AttributeError at /create_new Exception Value: type object 'BaseModel' has no attribute '_default_manager' Request information: GET: No GET data POST: csrfmiddlewaretoken = u'**' my_id = u'1' name = u'Rohit' FILES: No FILES data COOKIES: csrftoken = '' -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/IpjAFDReTPwJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: AttributeError for unique field during ModelForm validation
I want to ensure that a username is unique hence the unique constraint. Is having a ModelForm with an abstract model is supported? If not I will consider dynamically creating an instance of derived model form. But it looks a lot cleaner with the abstract class. On Saturday, November 10, 2012 5:12:18 PM UTC-5, Andrejus wrote: > > Hi! > I would try to set unique property within "real" model, not within > abstract base class. Besides django creates unique pk on each model by > default, so to my mind creating additional unique field is redundant. Not > quite sure, but there seems to be some restrictions on use of unique > property. > > воскресенье, 11 ноября 2012 г., 0:40:54 UTC+4 пользователь Rohit Banga > написал: >> >> I noticed a strange behavior with Django and filed a bug >> https://code.djangoproject.com/ticket/19271#ticket >> >> It is suggested that I first check on the support group if the bug is >> valid or not. Fair Enough. >> >> I have created a standalone project to demonstrate the problem. In order >> to run it you may have to create a database and configure it in settings.py >> though. >> Code that does not work: >> https://github.com/iamrohitbanga/django_ticket_19271/tree/code_not_working >> >> Code that works: >> https://github.com/iamrohitbanga/django_ticket_19271/tree/code_working >> >> Just creating my_id field in BaseModel class with unique=True set causes >> the code to fail with the stack trace below. >> My usecase is a bit weird hence you would find model form inheriting from >> abstract models. IMHO that should not matter. The code works without unique >> fields but does not work with non-unique fields. I will be happy to provide >> more clarifications. >> >> Below is the stacktrace for the >> "AttributeError at /create_new type object 'BaseModel' has no attribute >> '_default_manager' Traceback: File >> "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in >> get_response 111. response = callback(request, *callback_args, >> **callback_kwargs) File "django_bug_19271/testproj/testproj/views.py" in >> create_new 33. if form.is_valid(): File >> "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in is_valid >> 124. return self.is_bound and not bool(self.errors) File >> "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in >> _get_errors 115. self.full_clean() File >> "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in >> full_clean 272. self._post_clean() File >> "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in >> _post_clean 338. self.validate_unique() File >> "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in >> validate_unique 347. self.instance.validate_unique(exclude=exclude) File >> "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in >> validate_unique 633. errors = self._perform_unique_checks(unique_checks) >> File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in >> _perform_unique_checks 717. qs = >> model_class._default_manager.filter(**lookup_kwargs) Exception Type: >> AttributeError at /create_new Exception Value: type object 'BaseModel' has >> no attribute '_default_manager' Request information: GET: No GET data POST: >> csrfmiddlewaretoken = u'**' my_id = u'1' name = u'Rohit' FILES: No >> FILES data COOKIES: csrftoken = '' >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ZWttij8sGjQJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: AttributeError for unique field during ModelForm validation
Thanks Andrejus. Looks like you understand my usecase. I have to use multiple tables with the same schema which is why I am using inheritance. I am using abstract base models as each model should get its own table. If I use multi-table inheritance data from all these tables ends up in one table while creating a one-to-one relationship between the two tables. I must use abstract models. But I can consider creating model forms for the child tables and then loading the model form dynamically. I can now understand that the unique check on the abstract model would fail if the model form pointed to an abstract model. It does not know which table implementation to look at to verify if the unique constraint is satisfied. Thanks Rohit Banga http://iamrohitbanga.com/ On Sun, Nov 11, 2012 at 9:21 AM, Andrejus wrote: > > Unfortunately I haven't got experience with abstarct models, one thing I > clearly carried out form docs - abstract model has only one clear purpose > - to avoid duplicating the same fields and methods when writing code for > models. Abstract model is never transformed to database table, so may not > support all model's features in strightforward way. Try to make more simple > design wich may seem more clear for django. You can set unique key on one > or more more fields not only using unique property, but in Meta class as > well (it is used on real models when you want to set unique composite key). > I can advise to use multy-table model inheritance if you want to store > common set of fields in a distinct database table - this way I have > successfully used myself in my own project. It's simple, clear and > well-working method. Django authomatcally creates all necessary pk's and > fk's in the database, all models appear in admin site. And you can use > ModelForms on any model you want. > > воскресенье, 11 ноября 2012 г., 3:05:54 UTC+4 пользователь Rohit Banga > написал: > >> I want to ensure that a username is unique hence the unique constraint. >> Is having a ModelForm with an abstract model is supported? If not I will >> consider dynamically creating an instance of derived model form. But it >> looks a lot cleaner with the abstract class. >> >> On Saturday, November 10, 2012 5:12:18 PM UTC-5, Andrejus wrote: >>> >>> Hi! >>> I would try to set unique property within "real" model, not within >>> abstract base class. Besides django creates unique pk on each model by >>> default, so to my mind creating additional unique field is redundant. Not >>> quite sure, but there seems to be some restrictions on use of unique >>> property. >>> >>> воскресенье, 11 ноября 2012 г., 0:40:54 UTC+4 пользователь Rohit Banga >>> написал: >>>> >>>> I noticed a strange behavior with Django and filed a bug >>>> https://code.djangoproject.**com/ticket/19271#ticket<https://code.djangoproject.com/ticket/19271#ticket> >>>> >>>> It is suggested that I first check on the support group if the bug is >>>> valid or not. Fair Enough. >>>> >>>> I have created a standalone project to demonstrate the problem. In >>>> order to run it you may have to create a database and configure it in >>>> settings.py though. >>>> Code that does not work: >>>> https://github.com/**iamrohitbanga/django_ticket_** >>>> 19271/tree/code_not_working<https://github.com/iamrohitbanga/django_ticket_19271/tree/code_not_working> >>>> >>>> Code that works: >>>> https://github.com/**iamrohitbanga/django_ticket_** >>>> 19271/tree/code_working<https://github.com/iamrohitbanga/django_ticket_19271/tree/code_working> >>>> >>>> Just creating my_id field in BaseModel class with unique=True set >>>> causes the code to fail with the stack trace below. >>>> My usecase is a bit weird hence you would find model form inheriting >>>> from abstract models. IMHO that should not matter. The code works without >>>> unique fields but does not work with non-unique fields. I will be happy to >>>> provide more clarifications. >>>> >>>> Below is the stacktrace for the >>>> "AttributeError at /create_new type object 'BaseModel' has no attribute >>>> '_default_manager' Traceback: File "/usr/local/lib/python2.7/** >>>> dist-packages/django/core/**handlers/base.py" in get_response 111. >>>> response = callback(request, *callback_args, **callback_kwargs) File >>>> "django_bug_19271/testproj/**testproj/views.py" in crea