No worries. Wow, I've got some interesting results. I am pretty sure the ModelForm class has a bug in it that it is not looking at the secondary database with the using() method. It is a bug in the foreign reference fields only, though.
I set up tables in my default database, and in an extra database named staff, which is set up in settings.py. In default DB: n_test_staff username;nics_group "tsamuel";1 n_nics_groups n_group_number;n_group_name 1;"Group1" 2;"Group2" In staff DB: n_test_staff username;nics_group "tsamuel";2 (Notice tsamuel in group 2 in this DB) n_nics_groups n_group_number;n_group_name 1;"Staff1" 2;"Staff2" In the models, wrote __unicode__(self): method to show the groups. Here's something strange: >>> from models import Staff, NICSGroupType >>> from django.forms import ModelForm >>> class StaffForm(ModelForm): ... class Meta: ... model = Staff ... >>> staff = Staff.objects.using('staff').get(username='tsamuel') >>> print staff tsamuel <group: Staff2 > >>> form = StaffForm(instance=staff) >>> print form <tr><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" name="username" value="tsamuel" maxlength="50" /></td></tr> <tr><th><label for="id_nics_group">Nics group:</label></th><td><select name="nics_group" id="id_nics_group"> <option value="">---------</option> <option value="2" selected="selected">2: Group2</option> <option value="1">1: Group1</option> </select></td></tr> Notice this ModelForm is getting the Staff data from the 'staff' database, but is getting the reference n_nics_groups data from the default database's n_nics_groups table. Remember in staff tsamuel was part of group 2, which is the selected option, but the label is Group2 instead of Staff2, which is the value in n_nics_groups table in the 'staff' database. Then I tried deleting the default database n_test_staff table to verify: >>> from models import Staff, NICSGroupType >>> from django.forms import * >>> class StaffForm(ModelForm): ... class Meta: ... model = Staff ... >>> staff = Staff.objects.using('staff').get(username='tsamuel') >>> form = StaffForm(instance=staff) >>> print form <tr><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" name="username" value="tsamuel" maxlength="50" /></td></tr> <tr><th><label for="id_nics_group">Nics group:</label></th><td><select name="nics_group" id="id_nics_group"> <option value="">---------</option> <option value="2" selected="selected">2: Group2</option> <option value="1">1: Group1</option> </select></td></tr> Now, if I delete the default database's n_nics_groups table also, so it should all be using the 'staff' database, the only place n_test_staff and n_nics_groups exist now. >>> from models import Staff, NICSGroupType >>> from django.forms import * >>> class StaffForm(ModelForm): ... class Meta: ... model = Staff ... >>> staff = Staff.objects.using('staff').get(username='tsamuel') >>> print staff tsamuel <group: Staff2 > >>> form = StaffForm(instance=staff) >>> print form Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/lib/python2.5/site-packages/django/utils/encoding.py", line 27, in __str__ return self.__unicode__().encode('utf-8') File "/usr/lib/python2.5/site-packages/django/forms/forms.py", line 95, in __unicode__ return self.as_table() File "/usr/lib/python2.5/site-packages/django/forms/forms.py", line 217, in as_table errors_on_separate_row = False) File "/usr/lib/python2.5/site-packages/django/forms/forms.py", line 180, in _html_output 'field': unicode(bf), File "/usr/lib/python2.5/site-packages/django/forms/forms.py", line 408, in __unicode__ return self.as_widget() File "/usr/lib/python2.5/site-packages/django/forms/forms.py", line 439, in as_widget return widget.render(name, self.value(), attrs=attrs) File "/usr/lib/python2.5/site-packages/django/forms/widgets.py", line 516, in render options = self.render_options(choices, [value]) File "/usr/lib/python2.5/site-packages/django/forms/widgets.py", line 533, in render_options for option_value, option_label in chain(self.choices, choices): File "/usr/lib/python2.5/site-packages/django/forms/models.py", line 882, in __iter__ for obj in self.queryset.all(): File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 107, in _result_iter self._fill_cache() File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 772, in _fill_cache self._result_cache.append(self._iter.next()) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 273, in iterator for row in compiler.results_iter(): File "/usr/lib/python2.5/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter for rows in self.execute_sql(MULTI): File "/usr/lib/python2.5/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql cursor.execute(sql, params) File "/usr/lib/python2.5/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute return self.cursor.execute(query, args) DatabaseError: relation "n_nics_groups" does not exist This seems to be a bug in the ModelForm class that is not honoring the referenced fields that exist outside the default database. I tried appending the using('staff') to a few different places in the StaffForm class and also to the instance of StaffForm, but to no avail. Anybody else see this as not a bug? Furbee On Fri, Nov 4, 2011 at 1:45 PM, Tabitha Samuel <tabitha.sam...@gmail.com>wrote: > I don't mean to be spamming you like this, just thought you might be > interested in this result: > > So I created the same tables (n_test_staff and n_nics_groups) in the > default gibbs database. I removed the using part in the form statement > (form = StaffForm(instance = Staff.objects.using('gold').get(username > =current_staff), so now it looks like form = StaffForm(instance = > Staff.objects.get(username =current_staff) and it works fine, > everything is getting displayed in the view perfectly!! > > Problem is that those two tables have to live in the other database - > gold. Not sure how to proceed from here. I may just have to remove the > foreign key reference for the time being and update it manually in the > code, until a fix can be found for this. > > Tabitha > > On Nov 4, 3:19 pm, Tabitha Samuel <tabitha.sam...@gmail.com> wrote: > > I found this patch for the raw function (https:// > > code.djangoproject.com/attachment/ticket/13805/manager.patch), because > > according to this ticket (https://code.djangoproject.com/ticket/ > > 13805), .raw does not work in a multi db env. So this is what I have: > > > > in django/db/models/manager.py, I have created a new function: > > def raw(self, raw_query, params=None, using=None, *args, **kwargs): > > if using is None: > > using = self._db > > print using > > return RawQuerySet(raw_query=raw_query, model=self.model, > > params=params, using=using, *args, **kwargs) > > > > The print using returns "gold" so I know it is using this method. > > > > Now, I have > > form = StaffForm(instance = Staff.objects.raw("SELECT s.*, > > g.n_group_name FROM n_test_staff s LEFT JOIN n_nics_groups g > > ON(g.n_group_number = s.nics_group) WHERE s.username = > > 'tsamuel'",None,"gold")) > > in my view and I am still getting the error: > > AttributeError at /staff/staffinfo > > 'RawQuerySet' object has no attribute '_meta' > > > > This is the traceback: > > Traceback: > > File "/nics/a/applications/gibbs/python/site-packages_django/django/ > > core/handlers/base.py" in get_response > > 111. response = callback(request, > > *callback_args, **callback_kwargs) > > File "/nics/a/applications/gibbs/python/site-packages_django/django/ > > contrib/auth/decorators.py" in _wrapped_view > > 23. return view_func(request, *args, **kwargs) > > File "/nics/a/home/tsamuel/tssandbox/gibbs/utils/decorators.py" in > > decorate > > 11. return view_func(request, *args, **kws) > > File "/nics/a/home/tsamuel/tssandbox/gibbs/../gibbs/staff/views.py" in > > staff_info > > 156. form = StaffForm(instance = Staff.objects.raw("SELECT s.*, > > g.n_group_name FROM n_test_staff s LEFT JOIN n_nics_groups g > > ON(g.n_group_number = s.nics_group) WHERE s.username = > > 'tsamuel'",None,"gold")) > > File "/nics/a/applications/gibbs/python/site-packages_django/django/ > > forms/models.py" in __init__ > > 237. object_data = model_to_dict(instance, opts.fields, > > opts.exclude) > > File "/nics/a/applications/gibbs/python/site-packages_django/django/ > > forms/models.py" in model_to_dict > > 110. opts = instance._meta > > > > Exception Type: AttributeError at /staff/staffinfo > > Exception Value: 'RawQuerySet' object has no attribute '_meta' > > > > On Nov 4, 2:49 pm, Tabitha Samuel <tabitha.sam...@gmail.com> wrote: > > > > > > > > > > > > > > > > > One thought I have is that the using() is messing things up. Would it > > > be possible for you to create a second non-default db, throw these two > > > tables in there and then try the same thing? If that works for you, > > > then I must have a ghost sitting in my system! :-| > > > > > On Nov 4, 2:28 pm, Furbee <furbeena...@gmail.com> wrote: > > > > > > That is very strange... I recreated this on my development system > and it > > > > worked fine. I looked for Django bugs, but haven't found any related > to > > > > this issue. To be clear, the table n_nics_groups definitely exists > in the > > > > same database as n_test_staff, right? It does in mine because I used > python > > > > manage.py syncdb to create them from the models. If so, maybe we > should try > > > > to create a raw() query to see if the system has a bug creating the > query. > > > > This should be similar: > > > > > > form = StaffForm(instance = Staff.objects.raw("SELECT s.*, > g.n_group_name > > > > FROM n_test_staff s LEFT JOIN n_nics_groups g ON(g.n_group_number = > > > > s.nics_group) WHERE s.username = 'tsamuel')) > > > > > > If that throws an error, we may have a problem in the DB layer. > > > > > > Thanks, > > > > > > Furbee > > > > > > On Fri, Nov 4, 2011 at 10:29 AM, Tabitha Samuel < > tabitha.sam...@gmail.com>wrote: > > > > > > > The error that I'm getting is on the second form instantiation, > that > > > > > is on the line: > > > > > > > form = StaffForm(instance = > Staff.objects.using('gold').get(username = > > > > > current_staff) > > > > > where current_staff='tsamuel' for instance > > > > > > > This is the traceback of the error that I get when I do a print > form > > > > > right after getting the form: > > > > > Environment: > > > > > > > Request Method: GET > > > > > Request URL:http://watermelon.nics.utk.edu:8004/staff/staffinfo > > > > > > > Django Version: 1.3.1 > > > > > Python Version: 2.6.2 > > > > > Installed Applications: > > > > > ['django.contrib.auth', > > > > > 'django.contrib.contenttypes', > > > > > 'django.contrib.sessions', > > > > > 'django.contrib.sites', > > > > > 'gibbs.quartermaster', > > > > > 'gibbs.userportal', > > > > > 'gibbs.reports', > > > > > 'gibbs.events', > > > > > 'gibbs.job_stats', > > > > > 'gibbs.simulator', > > > > > 'gibbs.staff'] > > > > > Installed Middleware: > > > > > ('django.contrib.csrf.middleware.CsrfViewMiddleware', > > > > > 'django.middleware.common.CommonMiddleware', > > > > > 'django.contrib.csrf.middleware.CsrfResponseMiddleware', > > > > > 'django.contrib.sessions.middleware.SessionMiddleware', > > > > > 'django.contrib.auth.middleware.AuthenticationMiddleware') > > > > > > > Traceback: > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > core/handlers/base.py" in get_response > > > > > 111. response = callback(request, > > > > > *callback_args, **callback_kwargs) > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > contrib/auth/decorators.py" in _wrapped_view > > > > > 23. return view_func(request, *args, **kwargs) > > > > > File "/nics/a/home/tsamuel/tssandbox/gibbs/utils/decorators.py" in > > > > > decorate > > > > > 11. return view_func(request, *args, **kws) > > > > > File > "/nics/a/home/tsamuel/tssandbox/gibbs/../gibbs/staff/views.py" in > > > > > staff_info > > > > > 159. print form > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > utils/encoding.py" in __str__ > > > > > 27. return self.__unicode__().encode('utf-8') > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/forms.py" in __unicode__ > > > > > 95. return self.as_table() > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/forms.py" in as_table > > > > > 217. errors_on_separate_row = False) > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/forms.py" in _html_output > > > > > 180. 'field': unicode(bf), > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/forms.py" in __unicode__ > > > > > 408. return self.as_widget() > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/forms.py" in as_widget > > > > > 439. return widget.render(name, self.value(), attrs=attrs) > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/widgets.py" in render > > > > > 516. options = self.render_options(choices, [value]) > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/widgets.py" in render_options > > > > > 533. for option_value, option_label in chain(self.choices, > > > > > choices): > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/ > > > > > forms/models.py" in __iter__ > > > > > 882. for obj in self.queryset.all(): > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/db/ > > > > > models/query.py" in _result_iter > > > > > 107. self._fill_cache() > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/db/ > > > > > models/query.py" in _fill_cache > > > > > 772. > > > > > self._result_cache.append(self._iter.next()) > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/db/ > > > > > models/query.py" in iterator > > > > > 273. for row in compiler.results_iter(): > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/db/ > > > > > models/sql/compiler.py" in results_iter > > > > > 680. for rows in self.execute_sql(MULTI): > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/db/ > > > > > models/sql/compiler.py" in execute_sql > > > > > 735. cursor.execute(sql, params) > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/db/ > > > > > backends/util.py" in execute > > > > > 34. return self.cursor.execute(sql, params) > > > > > File > "/nics/a/applications/gibbs/python/site-packages_django/django/db/ > > > > > backends/postgresql_psycopg2/base.py" in execute > > > > > 44. return self.cursor.execute(query, args) > > > > > > > Exception Type: DatabaseError at /staff/staffinfo > > > > > Exception Value: relation "n_nics_groups" does not exist > > > > > > > Tabitha > > > > > > > On Nov 4, 11:31 am, Furbee <furbeena...@gmail.com> wrote: > > > > > > Oh no, that would not be a good thing to share! :-) That's a > bummer that > > > > > > the upgrade broke things. > > > > > > > > So, the error is being raised on this line? > > > > > > staff_form = StaffForm(request.POST, > > > > > instance=staffinstance).using('gold') > > > > > > > > Furbee > > > > > > > > On Fri, Nov 4, 2011 at 8:02 AM, Tabitha Samuel < > tabitha.sam...@gmail.com > > > > > >wrote: > > > > > > > > > So this is how I'm creating the staff form: > > > > > > > > > def staff_info(request, *args, **kws): > > > > > > > class StaffForm(forms.ModelForm): > > > > > > > class Meta: > > > > > > > model= Staff > > > > > > > .... > > > > > > > .... > > > > > > > .... > > > > > > > if request.method == 'POST' and request.POST['event'] == > > > > > > > 'choosestaff': > > > > > > > current_staff = > > > > > > > request.POST.get('selectstaff') > > > > > > > elif request.method == 'POST' and request.POST['event'] == > > > > > > > 'editstaff': > > > > > > > #print > > > > > > > request > > > > > > > current_staff = > > > > > > > request.POST.get('username') > > > > > > > errors = > > > > > > > validate_staff(request) > > > > > > > if errors is not None and len(errors) == > > > > > > > 0: > > > > > > > print... > > > > read more ยป > > -- > 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.