﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14948	Broken routers in 1.2.4: type object 'ModelBase' has no attribute '_meta'	shell_dweller	nobody	"There is a problem with using routers in the latest security release 1.2.4. The details are somewhat murky for a lay person but here's the gist of it:

* The site has a routing scheme setup as described in http://docs.djangoproject.com/en/1.2/topics/db/multi-db/#using-routers

* Attempt to save any model with a ManyToManyField fails in router when trying to execute db_for_write:

{{{
    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'my_app_label':
            return 'my_db_name'
        return None
}}}

Error message is ""type object 'ModelBase' has no attribute '_meta'"".

It seems that the error occurs in django/db/models/fields/related.py, line 624 where {{{ self.through.__class__ }}} returns ModelBase whereas prior to 1.2.4 it used to be a user defined class.


Here's the stack trace:

{{{
  [ snip ]

  File ""/usr/lib/python2.4/site-packages/django/forms/models.py"", line 375, in save
    fail_message, commit, construct=False)

  File ""/usr/lib/python2.4/site-packages/django/forms/models.py"", line 87, in save_instance
    save_m2m()

  File ""/usr/lib/python2.4/site-packages/django/forms/models.py"", line 83, in save_m2m
    f.save_form_data(instance, cleaned_data[f.name])

  File ""/usr/lib/python2.4/site-packages/django/db/models/fields/related.py"", line 1144, in save_form_data
    setattr(instance, self.attname, data)

  File ""/usr/lib/python2.4/site-packages/django/db/models/fields/related.py"", line 730, in __set__
    manager.clear()

  File ""/usr/lib/python2.4/site-packages/django/db/models/fields/related.py"", line 506, in clear
    self._clear_items(self.source_field_name)

  File ""/usr/lib/python2.4/site-packages/django/db/models/fields/related.py"", line 624, in _clear_items
    db = router.db_for_write(self.through.__class__, instance=self.instance)

  File ""/usr/lib/python2.4/site-packages/django/db/utils.py"", line 134, in _route_db
    chosen_db = method(model, **hints)

  File ""/path/to/my/app/routers.py"", line 10, in db_for_write
    if model._meta.app_label == 'my_label':

AttributeError: type object 'ModelBase' has no attribute '_meta'"		closed	Database layer (models, ORM)	1.2		fixed	router, ManyToManyField, blocker, regression	s.kuzmenko@… django@… fwenzel@… james@… conrad666@… typeshige@… brian@… django@… hgeerts@…	Accepted	1	0	0	1	0	0
