Ok...I've gotten this working a different way, in case anyone is
interested, I thought I'd post it here (although parent_company is
currently not saving for some reason..anyone?)

Here is my model code...again..notice that the Company has a related
Address.

class Address(models.Model):
    address_id = models.AutoField(primary_key=True)
    street_number = models.IntegerField(null=True, blank=True)
    street_name = models.CharField(blank=True, maxlength=135)
    street_suffix = models.TextField(blank=True)
    city = models.CharField(blank=True, maxlength=135)
    county = models.CharField(blank=True, maxlength=135)
    state = models.CharField(blank=True, maxlength=60)
    country = models.CharField(blank=True, maxlength=135)
    zip = models.CharField(blank=True, maxlength=30)
    update_user = models.ForeignKey(UserInfo)
#models.IntegerField(null=True, blank=True)
    time_stamp = models.DateTimeField()
    class Meta:
        db_table = 'address'

class Company(models.Model):
    company_id = models.AutoField(primary_key=True)
    parent_company = models.ForeignKey("self",unique=True,null=True)
    address = models.ForeignKey(Address,unique=True
    company_name = models.CharField(blank=True, maxlength=150)
    company_description = models.CharField(blank=True, maxlength=300)
    update_user =
models.ForeignKey(UserInfo)#models.IntegerField(null=True, blank=True)
    time_stamp = models.DateTimeField()
    def __str__(self):
        return self.company_name
    class Meta:
        db_table = 'company'



Ok..now my view code..I've subclassed changemanipulator

class CustomCompanyChangeManipulator(Company.ChangeManipulator):
    def __init__(self,company_id):
        all_companies = Company.objects.all()
        Company.ChangeManipulator.__init__(self,company_id)
        self.original_object =
Company.objects.get(company_id=company_id)
        allcomp = ()
        if all_companies:
           allcomp  = [(obj.company_id, obj.company_name) for obj in
all_companies]
        noneobj = ('','')
        allcomp.insert(0,noneobj)

        self.fields = (
            forms.SelectField(field_name="parent_company_id",
is_required=False,choices=allcomp),
            forms.TextField(field_name="company_name", length=20,
is_required=True),
            forms.TextField(field_name="company_description",
maxlength=255, length=30,is_required=True),
            forms.SelectField(field_name="street_prefix",
is_required=False,choices=STREET_PREFIX),
            forms.IntegerField(field_name="street_number"),
            forms.TextField(field_name="street_name", length=30,
maxlength=45),
            forms.SelectField(field_name="street_type",
is_required=False,choices=STREET_TYPE),
            forms.TextField(field_name="city", length=30,
maxlength=45),
            forms.SelectField(field_name="state",
is_required=True,choices=STATES),
            forms.TextField(field_name="zip",is_required=True),
            forms.CheckboxField(field_name="createoffice",
is_required=False),
        )
    def flatten_data(self):
         origcompany = self.original_object
         fields = dict([(field.name, getattr(origcompany,
field.attname)) for field in self.original_object._meta.fields])
         for field in origcompany.address._meta.fields:
                fields[field.name] = getattr(origcompany.address,
field.attname)
         return fields

    def save(self, new_data):
        origcompany = self.original_object
        origcompany.company_name = new_data['company_name']
        origcompany.company_description =
new_data['company_description']
        origcompany.parent_company = new_data['parent_company_id']
        origcompany.save()

        origaddress = origcompany.address
        origaddress.street_prefix = new_data['street_prefix']
        origaddress.street_number = new_data['street_number']
        origaddress.street_name = new_data['street_name']
        origaddress.street_type = new_data['street_type']
        origaddress.city = new_data['city']
        origaddress.state = new_data['state']
        origaddress.zip = new_data['zip']


Here is where I actually call the code to render the form...
def rendereditcompany(request,company_id):
    current_user = request.session.get('CurrentUser')
    if not current_user:
        return HttpResponseRedirect('/rsswww/login')
    nonfields = {'time_stamp': False,'address':True }
    manipulator = CustomCompanyChangeManipulator(company_id)
    company = manipulator.original_object
    if request.POST:
        # If data was POSTed, we're trying to create a new Place.
        new_data = request.POST.copy()
        # Check for errors.
        errors = manipulator.get_validation_errors(new_data)

        if not errors:
              manipulator.do_html2python(new_data)
              manipulator.save(new_data)

              return HttpResponseRedirect('/rsswww/companyadmin')
        else:
              print "errors"
              print manipulator.get_validation_errors(new_data)
              form = forms.FormWrapper(manipulator, new_data, errors)
              return render_to_response('editcompany.html', {'form':
form})


    else:
        # No POST, so we want a brand new form without any data or
errors.
        errors = new_data = {}
        new_data = manipulator.flatten_data()

    # Create the FormWrapper, template, context, response.
    form = forms.FormWrapper(manipulator, new_data, errors)
    return render_to_response('editcompany.html', {'form': form,
'Company':company})


--~--~---------~--~----~------------~-------~--~----~
 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to