Dynamic Model Saving and Admin Activation

32 views
Skip to first unread message

Karandeep Nagra

unread,
Jun 13, 2012, 4:46:27 PM6/13/12
to django...@googlegroups.com
So I've set up my Django site to create a dynamic model for every Server that is created.  So at the creation of server Bender, the model Bender_request is created.  But whenever I restart the built-in server, Bender_request disappears from the admin page.  I checked my database and there is no table for Bender_request even before I shut down the server.  Furthermore, even though Bender_requests shows up in the admin interface, it can't be clicked on.  So it seems there is no database table being created for Bender_requests.  My code is below.  Any clue on this situation?

def create_model(name, fields=None, app_label='', module='', options=None, admin_opts=None):
    class Meta:
        pass

    if app_label:
        setattr(Meta, 'app_label', app_label)

    if options is not None:
        for key, value in options.iteritems():
            setattr(Meta, key, value)
   
    attrs = {'__module__': module, 'Meta': Meta}
   
    if fields:
        attrs.update(fields)
   
    model = type(name, (models.Model,), attrs)
   
    if admin_opts is not None:
        class Admin(admin.ModelAdmin):
            pass
        for key, value in admin_opts.iteritems():
            setattr(Admin, key, value)
        admin.site.register(model, Admin)
   
    return model
   
class Server(models.Model):
    name = models.CharField(max_length=100, unique=True)
    address = models.IPAddressField(unique=True)
    def __unicode__(self):
        return  "%s (%s)" % (self.name, self.address)

def make_server_request(sender, instance, **kwargs):
    if 'created' in kwargs:
        if kwargs['created']:
            def give_unicode(self):
                return "%s %s (%s) requesting access to %s (%s)" % (self.user.first_name, self.user.last_name, self.user.username, self.server.name, self.server.address)
   
            requestname = '%s Request' % unicodedata.normalize('NFKD', instance.name).encode('ascii', 'ignore')
            fields = {
                'user': models.ForeignKey(User),
                'granted': models.BooleanField(default=False),
                'request_date': models.DateTimeField(auto_now_add=True),
                'change_date': models.DateTimeField(auto_now=True, auto_now_add=True),
                '__unicode__': give_unicode,
            }
           
            def approve_selected(self, request, queryset):
                queryset.update(granted=True)
                if rows_update == 1:
                    message_bit = "1 request was"
                else:
                    message_bit = "%s requests were" % rows_updated
                self.message_user(request, "%s successfully marked as approved." % message_bit)
               
            def deny_selected(self, request, queryset):
                queryset.update(granted=False)
                if rows_update == 1:
                    message_bit = "1 request was"
                else:
                    message_bit = "%s requests were" % rows_updated
                self.message_user(request, "%s successfully marked as denied." % message_bit)

            options = {}
            admin_options = {
                'list_display': ('user', 'request_date', 'granted',),
                'ordering': ('-request_date',),
                'search_fields': ('user',),
                'list_filter': ('request_date', 'user', 'granted', 'change_date',),
                'date_hierarchy': 'request_date',
                'readonly_fields': ('user', 'request_date', 'change_date',),
                'actions': (approve_selected, deny_selected,),
            }
           
            Request = create_model(name=requestname, fields=fields, app_label='Request', module='Request.models', options=None, admin_opts=admin_options)

models.signals.post_save.connect(make_server_request, sender=Server, dispatch_uid="mymodule")

Daniel Roseman

unread,
Jun 14, 2012, 4:49:46 AM6/14/12
to django...@googlegroups.com
On Wednesday, 13 June 2012 21:46:27 UTC+1, Karandeep Nagra wrote:
So I've set up my Django site to create a dynamic model for every Server that is created.  So at the creation of server Bender, the model Bender_request is created.  But whenever I restart the built-in server, Bender_request disappears from the admin page.  I checked my database and there is no table for Bender_request even before I shut down the server.  Furthermore, even though Bender_requests shows up in the admin interface, it can't be clicked on.  So it seems there is no database table being created for Bender_requests.  My code is below.  Any clue on this situation?

Well, no, the code you've posted doesn't create any tables. What makes you think it would? And why would you expect a dynamic model to persist across restarts?

My "clue on this situation" is simply to not do this.
--
DR.

 
Reply all
Reply to author
Forward
0 new messages