1. Michael Hall
  2. dynamo

Commits

Michael Hall  committed 4837900

Add admins for DynamicModel definitions and dynamic models

  • Participants
  • Parent commits da48eb3
  • Branches default

Comments (0)

Files changed (2)

File admin.py

View file
+from dynamo.models import DynamicApp, DynamicModel, DynamicModelField
+from django.contrib import admin
+
+def create_tables(modeladmin, request, queryset):
+    for m in queryset.all():
+        m.create()
+create_tables.short_description = "Create tables for selected Models"
+
+class DynamicAppAdmin(admin.ModelAdmin):
+    fields = ('name', 'verbose_name')
+    search_fields = ('name','verbose_name')
+    ordering = ('name',)
+    list_display = ('name', 'verbose_name')
+admin.site.register(DynamicApp, DynamicAppAdmin)
+
+class DynamicModelAdmin(admin.ModelAdmin):
+    fields = ('name', 'app', 'verbose_name')
+    search_fields = ('name','verbose_name')
+    ordering = ('app','name')
+    list_display = ('app', 'name', 'verbose_name')
+    list_filter = ('app',)
+    actions = [create_tables]
+    
+admin.site.register(DynamicModel, DynamicModelAdmin)
+
+class DynamicModelFieldAdmin(admin.ModelAdmin):
+    search_fields = ('name','verbose_name', 'field_type')
+    ordering = ('name',)
+    list_display = ('model', 'name', 'verbose_name')
+    list_filter = ('model',)
+admin.site.register(DynamicModelField, DynamicModelFieldAdmin)
+
+for model in DynamicModel.objects.all():
+    admin.site.register(model.as_model(), admin.ModelAdmin)

File models.py

View file
     'dynamic_field':                ('django.db.models', '_Field'),
 }
 
-DJANGO_FIELD_CHOICES = [(key, value) for key, value in DJANGO_FIELD_MAP.items()]
+DJANGO_FIELD_CHOICES = [(key, value[1]) for key, value in DJANGO_FIELD_MAP.items()]
 
 class DynamicApp(models.Model):
 
     app = models.ForeignKey(DynamicApp, related_name='models',
                             null=False, blank=False)
                             
-    def to_model(self):
+    def as_model(self):
         attrs = {}
         class Meta:
             app_label = self.app.name
         attrs['Meta'] = Meta
         attrs['__module__'] = 'dynamo.dynamic_apps.%s.models' % self.app.name
         for field in self.fields.all():
-            attrs[field.name] = field.to_field()
+            attrs[field.name] = field.as_field()
         return type(str(self.name), (models.Model,), attrs)
         
     def create(self, using=None):
         using = using or router.db_for_write(self.__class__, instance=self)
-        actions.create(self.to_model(), using)
+        actions.create(self.as_model(), using)
         
 class DynamicModelField(models.Model):
 
                             help_text=_('Short description of the field\' purpose'),
                             max_length=256, null=True, blank=True)
                             
-    def to_field(self):
+    def as_field(self):
         # return models.Field(attributes)
         if self.field_type in DJANGO_FIELD_MAP:
             module, klass = DJANGO_FIELD_MAP[self.field_type]