I have been stymied by this one for a day or two now.
I have a situation where I have a 'reference' copy of an application
which gets deployed as a new django site with it's own database, url's
etc.  I am trying to automate this deployment as follows:  Present a
list of existing sites (there are other options such as disabling the
site etc.) with a button to 'Add New Site'.
In response to add new site, I create a form based on a Client model
in the database which has a m2m relationship with a User model, I also
add 3 other models to the form including the django-site ( A 'year'
and some other basic configuration stuff) by adding to the form Fields
list and by adding a custom save method.
Now the tricky stuff... I want to maintain a master list of the sites
in the reference database but only the active site in the site
specific database, I am doing this via a 2-step process of first
invoking the custom save method with an argument of 'site-only' and
commit=True to add the site record to the reference database, I then
do a custom SQL CREATE DATABASE, GRANT etc., dynamically add the new
database to the DATABASES in the settings, call_command
syncdb(database=new_database) and then call the custom save with
commit=True and database=new_database.
Believe it or not, this is all working perfectly fine EXCEPT for the
m2m relationship with the user (oops, I should have mentioned that I
also select one or more admin users from the reference database to add
to the new database).

Here is the code I have for the custom save method:


    def save(self, commit=True, site_only=True, database='default'):
        if not self.site:
            self.site = Site(name=self.cleaned_data['name'],
                             domain='provision.tigertaxanalytics.com/'
+ self.cleaned_data['acronym'])
            if site_only:
                if commit:
                    self.site.save(using=database)
                return self.site
        else:
            self.client = super(ClientForm, self).save(commit=False)
            if commit:
                try:
                    self.site.save(using=database) # When we create
the client in the new database we also need to save the site with the
already known id
                except Exception, e:
                    pass # Mostly for debugging where we don't have
true multiple sites
                self.client.save(using=database)
                client_users = super(ClientForm,
self).save_m2m(commit=False)
                client_users.save(using=database)
                #for user in self.cleaned_data['users']:
                #    user.save(using=database)
                #    client_user = Client_Users(client_id =
self.client.pk, user_id = user.pk)
                #    client_user.save(using=database)
                self.provision = Provision(client = self.client,
                                          title =
self.cleaned_data['provision_title'])
                self.provision.save(using=database)
                self.tax_year = TaxYear(provision = self.provision,
                                       tax_year =
self.cleaned_data['tax_year'])
                self.tax_year.save(using=database)
                self.period = Period(tax_year = self.tax_year)
                self.period.save(using=database)
        return self.client

the error that I am getting is: " AttributeError: 'super' object has
no attribute 'save_m2m' " when I try and do the .save_m2m,  as far as
I can see I am trying to call save_mm2m of the base Form object after
doing a base Form.save(commit=False)

I guess I have a couple of issues that I haven't seen addressed
anywhere here:
  Is there actually a save_m2m(commit=False) ?  I don't see anyway
around the problem of saving into the new_database
  Is there another way to get at the default 'through' table, as you
see I tried to reproduce what I imagine save_m2m to be doing, but the
Client_Users model isn't present in the models.  If I take out the
save_m2m stuff it all works perfectly (at least from the database
analysis viewpoint) but I can't access the site without a user record
in the new_database.


Thank you.

-- 
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.

Reply via email to