Hey thanks a bunch for the workaround. Maybe I am doing something wrong, but this is what I'm getting when I implement it and it really seems weird..
in my view I have: staff_person = Staff.objects.using('gold').get(username=current_staff) form = StaffForm(instance = staff_person) group =NICSGroupType.objects.using('gold').get(n_group_number=staff_person.nics_group.n_group_number) form.nics_group = group print form.nics_group print form The first print gives me "3" which is correct. The print form on the other hand gives me the same: Exception Type: DatabaseError at /staff/staffinfo Exception Value: relation "n_nics_groups" does not exist If I comment out that print and render the template, I get pretty much the same error: Exception Type: TemplateSyntaxError at /staff/staffinfo Exception Value: Caught DatabaseError while rendering: relation "n_nics_groups" does not exist and the problem line in the template is: <tr><td>NICS Group: </ td><td>{{form.nics_group}} So the question is how can it print the group number when I do a print form.group_number but exception out when I do a print form? Tabitha On Nov 5, 11:26 am, Furbee <furbeena...@gmail.com> wrote: > As a workaround to this bug, you could get the group separately, and attach > all of the group fields to the staff form manually until a fix is > implemented. Something like (hasn't been tested, you may need to assign > each NICSGroupType field to a form field): > > staff = Staff.objects.using('gold').get(username=current_staff) > form = StaffForm(instance = staff) > group = > NICSGroupType.objects.using('gold').get(n_group_number=staff.nics_group.n_group_number) > > form.nics_group = group # this may attach all fields, but most likely > you'll have to attach them manually, since they would just be a goup, not > FormFields. > > Hopefully that will get you through this jam, until a fix is created. I > haven't written any patches before, but I will see if I can figure it out, > time permitting. > > Thanks, > > Furbee > > > > > > > > On Fri, Nov 4, 2011 at 3:36 PM, Furbee <furbeena...@gmail.com> wrote: > > 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 > > ... > > 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.