Adrian Holovaty wrote:
> On 9/18/05, Eric Walstad <[EMAIL PROTECTED]> wrote:
[...]
> > Is it possible to do what I want, move common fields to a super class,
> > without generating a table for that super class?
>
> It's not currently possible, but that exact functionality is on the
> to-do list: http://code.djangoproject.com/ticket/419 .
>
> Adrian

Hi Adrian,
I'm short on time, but I think I may have hacked up something that
works (for me) and passes the unit tests.

Eric.

[EMAIL PROTECTED] django_src]$ diff -u
django/core/meta/__init__.py.orig django/core/meta/__init__.py
--- django/core/meta/__init__.py.orig   2005-09-18 14:51:18.000000000
-0700
+++ django/core/meta/__init__.py        2005-09-18 15:59:20.000000000
-0700
@@ -412,6 +412,11 @@
         # attribute order.
         fields.sort(lambda x, y: x.creation_counter -
y.creation_counter)

+        # Should this class generate database tables (Default is Yes)?
+        # This has the ultimate effect of keeping this class out of
the _MODELS
+        # list.
+        create_table = not (meta_attrs.pop('no_table', False))
+
         # If this model is a subclass of another model, create an
Options
         # object by first copying the base class's _meta and then
updating it
         # with the overrides from this class.
@@ -673,7 +678,9 @@
             # contain this list:
             # [<class 'django.models.polls.Poll'>, <class
'django.models.polls.Choice'>]
             # Don't do this if replaces_module is set.
-            app_package.__dict__.setdefault('_MODELS',
[]).append(new_class)
+            # Exclude models where the user has set 'no_table = True'
+            if create_table:
+                app_package.__dict__.setdefault('_MODELS',
[]).append(new_class)

         # Cache the app label.
         opts.app_label = app_label
@@ -725,6 +732,7 @@
     def __repr__(self):
         return '<%s object>' % self.__class__.__name__

+
 ############################################
 # HELPER FUNCTIONS (CURRIED MODEL METHODS) #
 ############################################
[EMAIL PROTECTED] django_src]$ python tests/runtests.py
Running tests with database 'postgresql'
All tests passed.


experiment.py file:
from django.core import meta

class MyBaseClass(meta.Model):
    """This class will not result in any tables being generated by
django"""
    created_on = meta.DateTimeField(auto_now_add=True)
    modified_on = meta.DateTimeField(auto_now=True)
    class META:
        no_table = True

class MyDerived(MyBaseClass):
    """This class will have tables generated and will include the:
     - created_on and
     - modified_on fields
    which are inherited from the 'MyBaseClass' class.
    """
    name = meta.CharField(maxlength=25)
    class META:
       module_name = 'my_derived_class'

class MyOtherDerived(MyDerived):
    """This class will not result in any tables being generated by
django and
    it will include all the fields inherited from both 'MyBaseClass'
and
    'MyDerived' as well as those fields defined here.
    """
    color = meta.CharField(maxlength=25)
    class META:
        module_name = 'my_other_derived'
        # Explicitly set the no_table flag so that this class doesn't
result in
        # a table being created.
        no_table = True

class MyLastDerived(MyOtherDerived):
    """This class will have tables generated and will include the:
     - created_on and
     - modified_on fields
    which are inherited from the 'MyBaseClass' class,
     - name
    which is inherited from the 'MyDerived' class and
     - color
    which is inherited from the 'MyOtherDerived' class as well as those
fields
    defined here.
    """
    size = meta.IntegerField(choices=((0, 'small'),
                                      (1, 'medium'),
                                      (2, 'large'),
                                      ))
    class META:
        module_name = 'my_last_derived'


[EMAIL PROTECTED] django_src]$ django-admin.py sql experiment
BEGIN;
CREATE TABLE experiment_my_derived_class (
    id serial NOT NULL PRIMARY KEY,
    modified_on timestamp with time zone NOT NULL,
    created_on timestamp with time zone NOT NULL,
    name varchar(25) NOT NULL
);
CREATE TABLE experiment_my_last_derived (
    id serial NOT NULL PRIMARY KEY,
    color varchar(25) NOT NULL,
    modified_on timestamp with time zone NOT NULL,
    created_on timestamp with time zone NOT NULL,
    name varchar(25) NOT NULL,
    size integer NOT NULL
);

Reply via email to