You could just steal the field from the manipulator containing the  
references.

Wouldn't this work;

class Model1(models.Model):
      content = models.CharField(maxlength=100)
      blah = models.CharField(maxlength=100)
      ref_model2 = models.ForeignKey(Model2)

class UsageExample(CustomAddManipulator):
      def __init__(self):
          self.fields = [
              forms.LargeTextField(field_name='content',  
is_required=True),
              forms.LargeTextField(field_name='content2',  
is_required=True),
              forms.LargeTextField(field_name='blah', is_required=True),
              ]
         self.get_manipulators()

         for field in self.manipulators[0].fields:
             if field.field_name == 'ref_model2':
                 self.fields.append(field)

         self.models = (('Model1',('content', 'blah', 'ref_model2')),  
('Model2', ('content2', 'blah')))

Is that sufficient? I was thinking that a nice addition to  
CustomAddManipulator would be a default __init__(), that pulled in  
the manipulators for all the models and grabbed all the fields in  
those models. Then you could just define;

class Usage(CustomAddManipulator):
     models = (('Model1',('content', 'blah', 'ref_model2')),  
('Model2', ('content2', 'blah')))

And get all the necessary fields.

I just realized the issue that you were actually trying to bring up,  
which is how to define a relationship between the instance of Model1  
and Model2 being created. Since I'm just leveraging the  
AddManipulators tied to models I can't see a way of doing that and  
preserving the feel of a model-tied manipulator.

-Mikeal

On Aug 4, 2006, at 1:46 AM, Aidas Bendoraitis wrote:

>
> As far as I understand, these two models are absolutely separate (just
> haing the same field "blah"). What about models with relationships? I
> think, that is a tricky part. Especially, when the relationship is not
> obligatory. Do you have any ideas how to implement a generic way for
> that?
>
> Regards,
> Aidas Bendoraitis [aka Archatas]
>
> On 8/4/06, Mikeal Rogers <[EMAIL PROTECTED]> wrote:
>>
>> I have to update multiple models a lot via post, and I love the
>> django AddManipulators but they don't help that much out of the box
>> because they are tied to a specific model.
>>
>> I wrote this as a way to define CustomAddManipulators that relate
>> fields to multiple models and get all the benefit of the
>> AddManipulators tied to those models.
>>
>> Before i start using it widely, I wanted some input on the approach
>> and see if it's django-ish enough. fast enough, and if it's useful to
>> anyone else and I should bother trying to contribute it once
>> completed :)
>>
>> I tried to make it's usage after definition exactly like that of a
>> manipulator that was tied to a single specific model.
>>
>> -------------------
>>
>> class CustomAddManipulator(forms.Manipulator):
>>
>>          manipulators = []
>>
>>      def get_manipulators(self):
>>
>>          for model in self.models:
>>              exec('manipulator = %s.AddManipulator()' % model[0])
>>              self.manipulators.append(manipulator)
>>
>>      def breakup_data(self, data):
>>
>>          new_data = []
>>
>>          for model in self.models:
>>              new_model_data = {}
>>              for key in model[1]:
>>                  new_model_data[key] = data[key]
>>              new_data.append(new_model_data)
>>
>>          return new_data
>>
>>      def do_html2python(self, data):
>>
>>          if len(self.manipulators) is 0:
>>              self.get_manipulators()
>>
>>          new_data = self.breakup_data(data)
>>
>>          for manipulator in self.manipulators:
>>              manipulator.do_html2python(new_data
>> [self.manipulators.index(manipulator)])
>>
>>      def save(self, data):
>>          # Method will go through model definitions and do
>> appropriate saves.
>>
>>          if len(self.manipulators) is 0:
>>              self.get_manipulators()
>>
>>          new_data = self.breakup_data(data)
>>
>>          for manipulator in self.manipulators:
>>              manipulator.save(new_data[self.manipulators.index
>> (manipulator)])
>>
>> class UsageExample(CustomAddManipulator):
>>      def __init__(self):
>>          self.fields = (
>>              forms.LargeTextField(field_name='content',
>> is_required=True),
>>              forms.LargeTextField(field_name='content2',
>> is_required=True),
>>              forms.LargeTextField(field_name='blah',  
>> is_required=True),
>>              )
>>
>>          self.models = (('Model1',('content', 'blah')), ('Model2',
>> ('content2', 'blah')))
>>
>>
>>>
>>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---

Reply via email to